• Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

AltME groups: search

Help · search scripts · search articles · search mailing list

results summary

worldhits
r4wp15
r3wp107
total:122

results window for this page: [start: 1 end: 100]

world-name: r4wp

Group: #Red ... Red language group [web-public]
DocKimbel:
21-Dec-2012
I have added a new function type today: routine!. It allows to write 
a Red/System function in a Red program. The compiler will marshal 
(or type-cast) the arguments back and forth automatically.

Here is the Fibonacci example rewritten as a routine:

Red [ ]

fibonacci: routine [
    n          [integer!]
    return: [integer!]
][
    either n < 2 [
        n
    ][
        (fibonacci n - 1) + (fibonacci n - 2)
    ]
]


The function body is Red/System code, so it will run at full Red/System 
speed.


Integer! and logic! values are converted automatically, other Red 
datatypes are passed boxed but type-casted as Red/System counterparts 
(as defined in the Red runtime). Hint: floats will be converted automatically 
too.

So, passing and processing a block! series would look like this:

Red [ ]

add-one: routine [
    blk       [block!]
    return: [block!]
    /local value tail int
][
    value: HEAD(blk)
    tail: TAIL(blk)
	
    while [value < tail][
        if TYPE(value) = TYPE_INTEGER [
                int: as red-integer! value
                int/value: int/value + 1
        ]
        value: value + 1
    ]
    RETURN(blk)
]


I haven't yet released the code, it needs a bit more work, it should 
be ready by tomorrow.


The purpose of routine! datatype is to provide access to ultra-fast 
and low-level code for Red program in a simple way. The design is 
not yet fully set in stone, so suggestions and comments are welcome.
Gregg:
29-Apr-2013
rejoin: func [

 "Returns a new string (or same series type as block/1) made from 
 reduced values."
	block [block!]
	/local op
][
	either empty? block: reduce block [block] [
		op: either series? first block [:copy] [:form]
		append op first block next block
	]
]
DocKimbel:
30-Apr-2013
Added new #call compilation directive to enable calling Red functions 
from Red/System.

Syntax:
    #call [<red-fun-name> <arg1> <arg2> ...]

Notes:
- it can be used only in routines body or #system body block.

- only function! value can be invoked (refinements not supported).

- arguments are either literal values or Red/System global/local 
variables.

- type casting (to a Red internal datatype) is allowed in arguments 
(avoids wasting an extra variable).
Arnold:
27-Jun-2013
I stumbled upon a possible bug with local variables.
flexfun-s: function [s [string!] return: [string!]][return s]
flexfun-i: function [i [integer!] return: [integer!] ][return i]

flexfun: function [n [integer! float! string!] return: [string! integer! 
logic!] /local rv uitstr uitint][
    rv: type? n

    either "string" = rv [uitstr: flexfun-s n][uitint: flexfun-i n]
]

When I do not declare the uitstr and uitint local variables, the 
compiler makes a ~local extra and notices double declaration:
Compiling to native code... 

Script: "Red/System IA-32 code emitter" (none)

*** Compilation Error: duplicate variable definition in function 
exec/f_flexfun  

*** in file: %/Users/Arnold/data/develop/red/testscripts/flextst1.red 
*** at line: 126 
*** near: [func [/local ~n ~local ~rv ~local ~uitstr ~uitint] [
        push ctx194/values
Group: Announce ... Announcements only - use Ann-reply to chat [web-public]
MaxV:
25-Oct-2012
Rebol [Purpose: {make wikibook entry}
Author: "Max Vessi"
version: 1.0.0
]
my?: func [
    "Prints information about words and values."
    'word [any-type!]

    /local value args item type-name refmode types attrs rtype temp
][       
    temp:  copy ""
    if all [word? :word not value? :word] [word: mold :word]

    if any [string? :word all [word? :word datatype? get :word]] [
        types: dump-obj/match system/words :word
        sort types
        if not empty? types [
            print ["Found these words:" newline types]
            exit
        ]
        print ["No information on" word "(word has no value)"]
        exit
    ]
    type-name: func [value] [
        value: mold type? :value
        clear back tail value
        join either find "aeiou" first value ["an "] ["a "] value
    ]
    if not any [word? :word path? :word] [
        append temp reduce [mold :word "is" type-name :word]
        exit
    ]

    value: either path? :word [first reduce reduce [word]] [get :word]
    if not any-function? :value [

        append temp reduce [uppercase mold word "is" type-name :value "of 
        value: "]

        append temp either object? value [ reduce ["^/" dump-obj value] ] 
        [mold :value]
        exit
    ]
    args: third :value
    append temp  "= USAGE: = ^/ "

    if not op? :value [append temp reduce [ uppercase mold word " "] 
    ]
    while [not tail? args] [
        item: first args
        if :item = /local [break]

        if any [all [any-word? :item not set-word? :item] refinement? :item] 
        [
            append temp reduce [append mold :item " "]

            if op? :value [append temp reduce [append uppercase mold word " "]
	    value: none]
        ]
        args: next args
    ]
    append temp  "^/" 
    args: head args
    value: get word
    append temp "^/= DESCRIPTION: = ^/"
    either string? pick args 1 [
        append temp reduce [first args]
        args: next args
    ] [
        append temp "^/''(undocumented)''^/"
    ]

    append temp reduce [ "^/^/"uppercase mold word " is " type-name :value 
    " value."]
    if block? pick args 1 [
        attrs: first args
        args: next args
    ]
    if tail? args [exit]
    while [not tail? args] [
        item: first args
        args: next args
        if :item = /local [break]
        either not refinement? :item [

            all [set-word? :item :item = to-set-word 'return block? first args 
            rtype: first args]
            if none? refmode [
		append temp "^/= ARGUMENTS: =^/"
                refmode: 'args
            ]
        ] [
            if refmode <> 'refs [
                append temp "^/= REFINEMENTS: =^/"
                refmode: 'refs
            ]
        ]
        either refinement? :item [	   	  
            append temp reduce ["*'''" mold item "'''"]

            if string? pick args 1 [append temp reduce [" -- " first args] 
	    args: next args]
            append temp "^/"
        ] [
            if all [any-word? :item not set-word? :item] [
                if refmode = 'refs [append temp "*"]
                append temp reduce ["*'''" :item "''' -- "]

                types: if block? pick args 1 [args: next args first back args]

                if string? pick args 1 [append temp reduce [first args ""] 
		args: next args]
                if not types [types: 'any]
                append temp rejoin [" (Type: " types ")"]
                append temp "^/"
            ]
        ]
    ]
    if rtype [append temp reduce ["^/RETURNS:^/^-" rtype]]
    if attrs [
        append temp "^/= (SPECIAL ATTRIBUTES) =^/"
        while [not tail? attrs] [
            value: first attrs
            attrs: next attrs
            if any-word? value [
                append temp reduce  ["*'''" value "'''"]
                if string? pick attrs 1 [
                    append temp reduce [" -- " first attrs]
                    attrs: next attrs
                ]
                append temp "^/"
            ]
        ]
    ]
    editor temp
    exit
]
Group: Ann-Reply ... Reply to Announce group [web-public]
MaxV:
25-Oct-2012
Rebol [Purpose: {make wikibook entry}
Author: "Max Vessi"
version: 2.0.0
]
my?: func [
    "Prints information about words and values."
    'word [any-type!]

    /local value args item type-name refmode types attrs rtype temp
][       
    temp:  copy ""
    if all [word? :word not value? :word] [word: mold :word]

    if any [string? :word all [word? :word datatype? get :word]] [
        types: dump-obj/match system/words :word
        sort types
        if not empty? types [
            print ["Found these words:" newline types]
            exit
        ]
        print ["No information on" word "(word has no value)"]
        exit
    ]
    type-name: func [value] [
        value: mold type? :value
        clear back tail value
        join either find "aeiou" first value ["an "] ["a "] value
    ]
    if not any [word? :word path? :word] [
        append temp reduce [mold :word "is" type-name :word]
        exit
    ]

    value: either path? :word [first reduce reduce [word]] [get :word]
    if not any-function? :value [

        append temp reduce [uppercase mold word "is" type-name :value "of 
        value: "]

        append temp either object? value [ reduce ["^/" dump-obj value] ] 
        [mold :value]
        exit
    ]
    args: third :value
    append temp  "= USAGE: = ^/ "

    if not op? :value [append temp reduce [ uppercase mold word " "] 
    ]
    while [not tail? args] [
        item: first args
        if :item = /local [break]

        if any [all [any-word? :item not set-word? :item] refinement? :item] 
        [
            append temp reduce [append mold :item " "]

            if op? :value [append temp reduce [append uppercase mold word " "]
	    value: none]
        ]
        args: next args
    ]
    append temp  "^/" 
    args: head args
    value: get word
    append temp "^/= DESCRIPTION: = ^/"
    either string? pick args 1 [
        append temp reduce [first args]
        args: next args
    ] [
        append temp "^/''(undocumented)''^/"
    ]

    append temp reduce [ "^/^/"uppercase mold word " is " type-name :value 
    " value."]
    if block? pick args 1 [
        attrs: first args
        args: next args
    ]
    if tail? args [exit]
    while [not tail? args] [
        item: first args
        args: next args
        if :item = /local [break]
        either not refinement? :item [

            all [set-word? :item :item = to-set-word 'return block? first args 
            rtype: first args]
            if none? refmode [
		append temp "^/= ARGUMENTS: =^/"
                refmode: 'args
            ]
        ] [
            if refmode <> 'refs [
                append temp "^/= REFINEMENTS: =^/"
                refmode: 'refs
            ]
        ]
        either refinement? :item [	   	  
            append temp reduce ["*'''" mold item "'''"]

            if string? pick args 1 [append temp reduce [" -- " first args] 
	    args: next args]
            append temp "^/"
        ] [
            if all [any-word? :item not set-word? :item] [
                if refmode = 'refs [append temp "*"]
                append temp reduce ["*'''" :item "''' -- "]

                types: if block? pick args 1 [args: next args first back args]

                if string? pick args 1 [append temp reduce [first args ""] 
		args: next args]
                if not types [types: 'any]
                append temp rejoin [" (Type: " types ")"]
                append temp "^/"
            ]
        ]
    ]
    if rtype [append temp reduce ["^/RETURNS:^/^-" rtype]]
    if attrs [
        append temp "^/= (SPECIAL ATTRIBUTES) =^/"
        while [not tail? attrs] [
            value: first attrs
            attrs: next attrs
            if any-word? value [
                append temp reduce  ["*'''" value "'''"]
                if string? pick attrs 1 [
                    append temp reduce [" -- " first attrs]
                    attrs: next attrs
                ]
                append temp "^/"
            ]
        ]
    ]
    append temp "^/= Source code =^/"
    append temp  reduce ["<pre>" join word ": "]
    if not value? word [print "''undefined''" exit]
    either any [native? get word op? get word action? get word] [
        append temp reduce ["native" mold third get word]
    ] [append temp reduce  [ mold get word "</pre>"] ]
    editor temp
    ;write clipboard://  temp
    exit
]
Group: Rebol School ... REBOL School [web-public]
Gregg:
24-Apr-2012
parse-int-values: func [

    "Parses and returns integer values, each <n> chars long in a string."
    input [any-string!]

    spec [block!] "Dialected block of commands: <n>, skip <n>, done, 
    char, or string"
    /local
        gen'd-rules ; generated rules
        result      ; what we return to the caller

        emit emit-data-rule emit-skip-rule emit-literal-rule emit-data
        digit= n= literal=
        int-rule= skip-rule= literal-rule= done= build-rule=
        data-rule skip-rule
][

    ; This is where we put the rules we build; our gernated parse rules.
    gen'd-rules: copy []
    ; This is where we put the integer results
    result: copy []

    ; helper functions

    emit: func [rule n] [append gen'd-rules replace copy rule 'n n]
    emit-data-rule: func [n] [emit data-rule n]
    emit-skip-rule: func [n] [emit skip-rule n]
    emit-literal-rule: func [value] [append gen'd-rules value]
    emit-data: does [append result to integer! =chars]

    ; Rule templates; used to generate rules

    ;data-rule: [copy =chars n digit= (append result to integer! =chars)]
    data-rule: [copy =chars n digit= (emit-data)]
    skip-rule: [n skip]

    ; helper parse rules
	digit=: charset [#"0" - #"9"]
    n=: [set n integer!]
    literal=: [set lit-val [char! | any-string!]]

    ; Rule generation helper parse rules
    int-rule=: [n= (emit-data-rule n)]
    skip-rule=: ['skip n= (emit-skip-rule n)]
    literal-rule=: [literal= (emit-literal-rule lit-val)]
    done=: ['done (append gen'd-rules [to end])]

    ; This generates the parse rules used against the input

    build-rule=: [some [skip-rule= | int-rule= | literal-rule=] opt done=]


    ; We parse the spec they give us, and use that to generate the

    ; parse rules used against the actual input. If the spec parse

    ; fails, we return none (maybe we should throw an error though);

    ; if the data parse fails, we return false; otherwise they get
    ; back a block of integers. Have to decide what to do if they
    ; give us negative numbers as well.
    either parse spec build-rule= [
        either parse input gen'd-rules [result] [false]
    ] [none]
]
Maxim:
27-Aug-2012
the local values are reset to none at each execution, but they will 
be re-assigned to the same litteral.
Ladislav:
27-Aug-2012
the local values are reset to none at each execution
 - that should have been:


the local variables are reset to NONE at every function call. After 
the function returns, the local variables are:
- in R2 they 
remember" the last (outermost call) value
- in R3 their values are inaccessible
BrianH:
27-Aug-2012
Sorry, didn't get that. In R2 if you are using functions not defined 
by the CLOSURE function then you might want to set some local variables 
to none before returning - that's what ALSO was for, btw. In R3 you 
don't need to unset anything because the GC will collect the set 
of values associated with the function's local variables after the 
function returns, or the whole context after a closure returns.
Maxim:
27-Aug-2012
In R3 ... the GC will collect the set of values associated with the 
function's local variables
   


does it actually perform the GC, actively mark them as potentials, 
or simply unreference them?
Andreas:
16-Nov-2012
Basically, you create an HTML file and put it on your webserver. 
Let's call it "form.html":


<form action="form.cgi"><input type="text" name="word"><input type="submit"></form>


Then you create a REBOL CGI matching the "action" used above, so 
"form.cgi", and also put it on your webserver:

#!/usr/local/bin/rebol278 -cs
REBOL []
cgi-values: construct decode-cgi system/options/cgi/query-string


;; Now you can access the "word" value submitted via the HTML form
;; as cgi-values/word.

;; Let's echo the value back to the user, as an example:
print rejoin [
  "Content-type: text/html" crlf
  crlf
  cgi-values/word
]
Gregg:
28-May-2013
parse-int-values: func [

    "Parses and returns integer values, each <n> chars long in a string."
    input [any-string!]

    spec [block!] "Dialected block of commands: <n>, skip <n>, done, 
    char, or string"
    /local
        gen'd-rules ; generated rules
        result      ; what we return to the caller

        emit emit-data-rule emit-skip-rule emit-literal-rule emit-data
        digit= n= literal=
        int-rule= skip-rule= literal-rule= done= build-rule=
        data-rule skip-rule
][

    ; This is where we put the rules we build; our gernated parse rules.
    gen'd-rules: copy []
    ; This is where we put the integer results
    result: copy []

    ; helper functions

    emit: func [rule n] [append gen'd-rules replace copy rule 'n n]
    emit-data-rule: func [n] [emit data-rule n]
    emit-skip-rule: func [n] [emit skip-rule n]
    emit-literal-rule: func [value] [append gen'd-rules value]
    emit-data: does [append result to integer! =chars]

    ; Rule templates; used to generate rules

    ;data-rule: [copy =chars n digit= (append result to integer! =chars)]
    data-rule: [copy =chars n digit= (emit-data)]
    skip-rule: [n skip]

    ; helper parse rules
	digit=: charset [#"0" - #"9"]
    n=: [set n integer!]
    literal=: [set lit-val [char! | any-string!]]

    ; Rule generation helper parse rules
    int-rule=: [n= (emit-data-rule n)]
    skip-rule=: ['skip n= (emit-skip-rule n)]
    literal-rule=: [literal= (emit-literal-rule lit-val)]
    done=: ['done (append gen'd-rules [to end])]

    ; This generates the parse rules used against the input

    build-rule=: [some [skip-rule= | int-rule= | literal-rule=] opt done=]


    ; We parse the spec they give us, and use that to generate the

    ; parse rules used against the actual input. If the spec parse

    ; fails, we return none (maybe we should throw an error though);

    ; if the data parse fails, we return false; otherwise they get
    ; back a block of integers. Have to decide what to do if they
    ; give us negative numbers as well.
    either parse spec build-rule= [
        either parse input gen'd-rules [result] [false]
    ] [none]
]
Group: Databases ... group to discuss various database issues and drivers [web-public]
Pekr:
12-Nov-2012
Create 2 files. Call the first one e.g. cgi-test.html, and upload 
it to your server. The only thing you have to change is the link 
to your .cgi script in there:

<HTML>
<TITLE>Simple Web Form</TITLE>
<BODY>
<b>Simple Web Form</b><p>
<FORM ACTION="http://www.xidys.com/cgi-bin/cgi-test.cgi">
<INPUT TYPE="TEXT" NAME="Field" SIZE="25"><BR>
<INPUT TYPE="SUBMIT" NAME="Submit" VALUE="Submit">
</FORM>
</BODY>
</HTML>



Create a second file, called cgi-test.cgi (it has to align to how 
you name it in the above source file). Upload it to your cgi working 
directory. Remember to change the first line to contain the path, 
where your REBOL executable is placed:

#!/usr/local/bin/rebcmd -sqc

REBOL []

print join "Content-type: text/plain" newline
start: now/time/precise

submitted: decode-cgi read-cgi
values: construct submitted

prin "Submitted: " print mold submitted
prin "values: " print mold values
prin "values/field: " print mold values/field

print now/time/precise - start
print newline
 

Now go to your URL, and try to submit some values. You can test it 
on my site at: http://www.xidys.com/cgi-test.html
Group: !REBOL3 ... General discussion about REBOL 3 [web-public]
BrianH:
19-Jan-2013
As for that blog, the behavior described there has some practical 
problems. There is nothing in the code itself (I mean the example 
code in the blog, not the implementation code) to indicate that 'last-stock 
is a free variable, but 'if and 'not are not - they are all words 
not declared at the top level of the module (by using them as set-words, 
like in an object) or elsewhere in the code. This means that if you 
want it to have 'last-stock be made local to a module, you would 
have to make 'if and 'not local as well. That works if lib words 
can only be set once, but not if they can be reset, since changes 
wouldn't propagate to the other modules or user scripts (since those 
values are copied to words in other contexts).


We determined that the behavior described in that blog could be useful 
enough to be worth supporting, but had some nasty side effects that 
made it not be what we wanted to do by default. That is why we made 
it an option, in particular the isolate option. If you specify the 
isolate option, your module acts like it does in the blog, and this 
has the effect of isolating your module from all external changes 
to the lib context.

world-name: r3wp

Group: RAMBO ... The REBOL bug and enhancement database [web-public]
Ladislav:
26-Jan-2007
I show you something from my article:

a: b: charset [#"a" #"b"] c: insert charset [#"a"] #"b
identical?: func [
    {are the values identical?}
    a [any-type!]
    b [any-type!]
    /local var var2
] [
    ; compare types

    if not-equal? type? get/any 'a type? get/any 'b [return false]
    ; there is only one #[unset!] value
    unless value? 'a [return true]
    ; errors can be disarmed and compared afterwards
    if error? :a [a: disarm :a b: disarm :b]
    ; we need to be transitive for decimals and money
    if any [decimal? :a money? :a] [
        return found? all [same? a b zero? a - b]
    ]
    ; we need to be transitive for dates

    if date? :a [return found? all [same? a b same? a/time b/time]]
    ; we need to be able to compare even the closed ports
    if port? :a [return equal? reduce [a] reduce [b]]
    ; our function has to work for structs
    if struct? :a [return same? third a third b]
    ; we can have something stronger than SAME? for bitsets
    if bitset? :a [
        unless same? a b [return false]
        if 0 = length? a [return true]
        unless equal? var: find a 0 find b 0 [return false]
        either var [
            remove/part a 0
            var2: find b 0
            insert a 0
        ] [
            insert a 0
            var2: find b 0
            remove/part a 0
        ]
        return var <> var2 
    ]
    same? :a :b
]
identical? a b ; == true
identical? a c ; == false
Group: Core ... Discuss core issues [web-public]
JaimeVargas:
7-Apr-2005
If anyone ever wanted multi-methods or function overload in rebol 
here is the answer. Enjoy ;-)

REBOL []

define-method: func [
	'name [word!] spec [block!] locals [block!] code [block!]

 /local w type-rule spec-rule continue? register-name methods-name
][
	;; first validate the spec
	continue?: [none] ;used to stop parsing

 type-rule: [set w word! (unless datatype? attempt [get w] [continue?: 
 [end skip]])]
	spec-rule: [some [word! into [type-rule continue?]]]
    unless parse spec spec-rule [make error! "invalid spec"]

	register-name: to-word join :name '-register
	methods-name: to-word join :name '-methods?
	unless value? name [
		
		context [
			dispatch-table: copy []
			
			spec-fingerprint: func [spec [block!] /local types][
				types: copy []
				foreach itm extract/index spec 2 2 [insert tail types itm/1 ]
				types
			]
			
			values-fingerprint: func [values [block!] /local types][
				types: copy []
				foreach v values [insert tail types type?/word v]
				types
			]
			

   retrieve-func: func [values [block!]][select/only dispatch-table 
   values-fingerprint values]
			
			set :name func [values [block!]][
				do compose [(retrieve-func values) (values)]
			]
			
			set :register-name func [spec code /local fingerprint pos][
				fingerprint: spec-fingerprint spec
				either found? pos: find/only dispatch-table fingerprint [
					poke dispatch-table 1 + index? pos function spec locals code
				][

     insert tail dispatch-table reduce [fingerprint function spec locals 
     code]
				]
			]
			
			set :methods-name does [probe dispatch-table]
		]
	]

	do reduce [register-name spec code]
]

define-method f [x [integer!]] [] [x + 1]
define-method f [s [block!]] [] [attempt [pick s 2]]
define-method f [x [decimal!]] [] [sine x] 

f[5] == 6
f[[one two three]] == two
f[90.0] == 1.0
Ingo:
22-Sep-2005
Why do I get an error "invalid argument" here?


>>       comp-length: func [a b][compare (length? a/2) (length? b/2)]
>>       sort/skip/compare files 2 :comp-length
** Script Error: Invalid argument: 2
** Near: sort/skip/compare files 2 :comp-length
>> source compare
compare: func [

    {compares to values, and returns -1 / 0 / 1 for values a<b / a=b 
    / a>b}
    a b
    /local return
][
    case [
        a > b [-1]
        a < b [1]
        true [0]
    ]
]

REBOL/View 1.3.1.3.1
Sunanda:
22-Sep-2005
It works for me:
files: copy  [%abc [%a/ %xx/] %def [%xyz/]]
compare: func [

        {compares to values, and returns -1 / 0 / 1 for values a<b / a=b 
        / a>b}
        a b
        /local return
    ][
        case [
                a > b [-1]
                a < b [1]
                true [0]
            ]
    ]


print system/version
sort/compare/all/skip files  :compare 2
probe files

1.3.1.3.1
== [%def [%xyz/] %abc [%a/ %xx/]]
MichaelB:
15-Dec-2005
this is from the blog-chat: 

I liked Ladislavs function and just extended it a little bit: maybe 
bind would be nice like that - one can bind only the words one wants 
to and also only the types one likes, unless using plain, then all 
words of the same spelling get bound

old-bind: :bind

bind: func append copy third :bind [
	/only only-words [block! any-word!]
    /plain
    /local pos rule item
][
	if copy [words: copy words]
		
	either only [
		if any-word? only-words [only-words: reduce [only-words]]
    	if any-word? known-word [known-word: bind? known-word]

     if plain [forall only-words [change only-words to word! only-words/1]]

		parse words rule: [
			any [
				pos:
				set item any-word! (
					if any [
                	    all [plain find only-words to word! item]
                	    find only-words item
                	][
						item: old-bind :item known-word
						change pos :item
					]
				) | into rule | skip
			]
		]	
	][
		old-bind words known-word
	]	
]

f: g: h: i: 1
bl: ['f g h i]
c: context [f: 2 g: 3 h: 'hello]

bind/only bl c [f 'h]
get-values: [
	get to-word first bl
	get to-word second bl
	get to-word third bl
	get to-word fourth bl
]
probe reduce get-values

bind/only/plain bl c [f 'h]
probe reduce get-values

bind bl 'system
probe reduce get-values

bind/only bl c 'g
probe reduce get-values
JaimeVargas:
29-Dec-2005
Rebol []

comment [
	; example usage:
	kernel: load/library %kernel32.dll

 routine-call kernel "MulDiv" [int] [3 [integer!] 2 [integer!] 1 [integer!]] 
 ; == 6
]

routine-call: func [
	library [library!]
	routine-name [string!]
	return-spec [block!]
	arguments [block!] 

 /typed {Arguments is block structure is: [argument-value [datatype] 
 ...]}
	/local routine spec call argument type typed-rule
] [
	spec: make block! length? arguments
	call: make block! (length? arguments) / 2 + 1
	insert call [return routine]
	typed-rule: copy []
	if typed [typed-rule: [set type skip]]
	parse reduce arguments [
		any [
			set argument skip
			typed-rule
			(
				insert/only tail spec 'argument
				insert/only tail spec either typed [
					type
				][
					reduce [type?/word get/any 'argument]
				]
				insert/only tail call get/any 'argument
			)
		]
	]
	insert tail spec [return:]
	insert/only tail spec return-spec
	routine: make routine! spec library routine-name
	do call
]

use [libc zero-char as-rebol-string malloc][
	libc: load/library %/usr/lib/libc.dylib ; osx variable

	zero-char: #"^@"

	as-rebol-string: func [
		[catch]
		s [string!] 
		/local pos
	][

  unless pos: find s zero-char [throw make error! "s is not a c-string"]
		s: head remove/part pos tail s
		replace/all s "\n" newline
		replace/all s "\t" tab
	]
	
	malloc: func [
        size [integer!] "size in bytes"
    ][
        head insert/dup copy {} zero-char size
    ]

	sprintf: func [
		spec {block structure is: [format values ...]}
		/local s
	][
		s: malloc 4096
		insert/only head spec 's
		routine-call libc "sprintf" [int] spec
		as-rebol-string s
	]
	
	printf: func [
		spec {block structure is: [format values ...]}
	][
		print sprintf spec
	]
]
Gregg:
12-Apr-2006
; used in SHIFT below
    dup: func [value len [integer!] /local type] [

        type: either series? value [value] [either char? value [""] [[]]]
		head insert/only/dup make type len value len
    ]

    ; used in SHIFT below
    make-blank-value: func [type] [
        any [
            attempt [make type 0]
            attempt [make type ""]
            attempt [make type []]
            attempt [make type none]
        ]
    ]


    ; The new PAD/JUSTIFY func might be used to implement this as well.
    shift: func [
        "Shift values in a series; length doesn't change."
        series [series!]
        /left   "Shift left (the default)"
        /right  "Shift right"

        /part range [number!] "Shift this many positions"  ; TBD series! 
        support?
        /with fill "Fill vacated slots with this value"
        /local pad
    ][
        range: any [range 1]
        if any [empty? series  0 = range] [return series]
        pad: dup any [fill  make-blank-value last series] range
        either right [

            head insert head clear skip tail series negate range pad
        ][
            append remove/part series range pad
        ]
    ]

    rotate: func [
        "Rotate values in a series."
        series [series!]
        /left   "Rotate left (the default)"
        /right  "Rotate right"

        /part range [number!] "Rotate this many positions"  ; TBD series! 
        support?
        /local offset pad
    ][
        range: any [all [range  range // length? series] 1]
        if any [empty? series  zero? range] [return series]
        either right [
            offset: does [skip tail series negate range]
            pad: copy offset
            head insert head clear offset pad
        ][
            pad: copy/part series range
            append remove/part series range pad
        ]
    ]
BrianH:
18-Jul-2006
Environment variables are based on values of the following registry 
keys, for each type:

- Local machine: HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment
- Current user: \Environment
- Default user: HKEY_USERS\.DEFAULT\Environment

- Volatile (probably should be treated as readonly): HKCU\Volatile 
Environment
BrianH:
18-Jul-2006
You can check in regedit for your current values. Remember to use 
REG_EXPAND_SZ values if you want references to other environment 
variables to be expanded, but keep in mind that these are evaluated 
in one pass for each category, and that local machine is evaluated 
before current user. A value can't make references to other variables 
in its own category, just references to values in other categories 
that are evaluated earlier.
Maxim:
20-Oct-2006
cause functions retain the pointers even if they use local values.
Jerry:
20-Oct-2006
To Gregg,

The diff algorithm I am using ... 

2 blocks, one for reg-data-old (block1), the other for reg-data-new 
(block2).
data in these blocks are in the following format:
   [ key1 value1 key2 value2 key3 value3 ... ]
   where keyX and valueX are both strings. 
Example:

   [ "HKEY_LOCAL_MACHINE_SOFTWARE_ABC"  {"sid"=dword:00000001^/"tid"=dword:000000FF} 
   ... ]


I use "SORT/SKIP 2" to sort the 2 blocks. It's very fast, I guess 
that's because the original data are in order already. After sorting, 
I can comapre these two blocks with the "race" algorithm. 

The "race" algorithm is very simple ...

loop [
    if ... the key in block1 is equal to the key in block2 
    then ... check their values (different values mean modified) 

    if ... the key in block1 is less than the key in block2

    then ... the key in block1 is deleted-key. Move the key in block 
    1 to the next key.  

    if ... the key in block1 is greater than the key in block2

    then ... the key in block2 is added-key. Move the key in block 2 
    to the next key.  
] 


Well, my English is not very good. I hope you understand what I am 
saying here.
Gregg:
16-Aug-2007
Another thing to consider is that this is a general need, so FOREACH 
(e.g.) may be used, but you can hide it in a wrapper func, maybe 
called SELECT-ALL, that works like REMOVE-EACH. I have different 
variations, based on how other langs do it, e.g. select/inject in 
smalltalk. Here's a very quick way to leverage REVMOE-EACH.

    filter: keep-each: func [

        "Keeps only values from a series where body block returns TRUE."

        'word  [get-word! word! block!] "Word or block of words to set each 
        time (will be local)"
        series [series!] "Series to traverse"

        body   [block!]  "Block to evaluate. Return TRUE to collect."
    ] [
        remove-each :word series join [not] to paren! body
    ]
    comment {
        filter x [1 2 3] [x = 2]
        filter x [1 2 3] [odd? x]
        filter res [1 2 3] [odd? res]
        filter [x y] [a 1 b 2 c 3] [all [odd? y  'c = x]]
    }
Oldes:
31-Jan-2008
>> ?? mod
mod: func [
    "Compute a nonnegative remainder of A divided by B."
    [catch]
    a [number! money! time!]
    b [number! money! time!] "Must be nonzero."
    /local r
][
    all [negative? r: a // b r: r + b]
    a: abs a
    either all [a + r = (a + b) positive? r + r - b] [r - b] [r]
]
>> ?? modulo
modulo: func [

    {Wrapper for MOD that handles errors like REMAINDER. Negligible
^-^-values (compared to A and B) are rounded to zero.}
    [catch]
    a [number! money! time!]
    b [number! money! time!] "Absolute value will be used"
    /local r
][
    throw-on-error [
        any [number? a b: make a b]
        r: mod a abs b
        either any [a - r = a r + b = b] [make r 0] [r]
    ]
]
[unknown: 5]:
22-Mar-2008
;Here is a handy skip function:

skip+: func [
    {Returns a series matching the skip sequence}
    series [series!] "Series to return skip values from."
    interval [integer!] "Skip interval"
    start [integer!] "Series index to start skipping from."
    /local blk][
    blk: copy []
    series: at series start
    while [not tail? series][

        if (index? series) = start [insert tail blk first series start: start 
        + interval]
        series: next series
    ]
    series: head series
    if empty? blk [return none]
    blk
]
[unknown: 5]:
23-Mar-2008
replace-all: func [
    "Replaces all occurences of old value with new value"
    series "Series containing values to replace"
    oldval "Value to be replaced"
    newval "New value to replace old value"
    /local subf
    ][
        subf: func [sd][
            while [not tail? sd ][  
                case [
                    equal? first sd :oldval [poke sd 1 :newval]
                    series? first sd [subf first sd]
                ]
                sd: next sd
            ]
        ]
        subf series
        series
    ]
[unknown: 5]:
23-Mar-2008
replace-all: func [
    "Replaces all occurences of old value with new value"
    series [series!] "Series containing values to replace"
    oldval [any-type!] "Value to be replaced"
    newval [any-type!] "New value to replace old value"
    /local subf
    ][
        subf: func [sd][
            while [not tail? sd ][  
                case [
                    equal? first sd :oldval [poke sd 1 :newval]
                    series? first sd [subf first sd]
                ]
                sd: next sd
            ]
        ]
        subf series
        series
    ]
[unknown: 5]:
23-Mar-2008
replace-all: func [
    "Replaces all occurences of old value with new value"
    series [series!] "Series containing values to replace"
    old-value  "Value to be replaced"
    new-value  "New value to replace old value"
    /local subf
    ][
        subf: func [sd][
            while [not tail? sd ][  
                case [

                    equal? first sd :old-value [poke sd 1 :new-value]
                    series? first sd [subf first sd]
                ]
                sd: next sd
            ]
        ]
        subf series
        series
    ]
RobertS:
1-Apr-2008
Diss'ing IDE's might alienate some Smalltalk folk.  I cannot imagine 
maintaining an application suite such as I deal with everyday without 
an IDE.  I just wish it was not eclipse ...  Of course only wimps 
used a Disk Operating System and real men code in machine codes only 
... and real pro's dictated their SNOBOL punch cards to lovely assistants 
...  and ANT scripts are for sissies.  Some must have ridiculed Tcl, 
Expect and TK in their day ... but if my IDE can facilitate my efforts 
to systematically (key word there) shrirnk company''s codebase as 
it becomes more reliable with better test coverage then maybe a refactoring 
browser would be a good tool after all.  Even better if it is an 
integrated part of the IDE, as in Dolphin Smalltalk or Squeak Smalltalk 
or Smalltalk/X or Cincom Visual Smalltalk.  Not that I couldn't survive 
on grep and diff's.  But once the codebase is too large for any one 
person to author or maintain on their lonesome, a tool that remebers 
what you did last and where can be a god-send.  If you want to know 
hell without an IDE join an actuarial department working in APL. 
 There you don't even know if they have talent: you just hope most 
of it works as each quarter rolls around and try to survive year-end. 
 But you know they're smart, cuz after all, they're actuaries - and 
look at all that APL code in all those files ... of course a few 
of them look back wistfully at their student days in C with Borland's 
decent IDE.  REBOL [
    File: %vid-usage.r
    Date: 09-Jan-2004   
    Title: "VID Usage"
    Purpose: "VID Usage Tutorial with Runnable Examples"
    Version: 1.2.1
    Author: "Cybarite"
    Edits: RobertS
    Source: {
        Based on %easy-vid.r by Carl Sassenrath.

        Clips from various sites including email that are attributed in the 
        section
        }
    library: [
        level: 'intermediate
        platform: 'all
        type: [tutorial]
        domain: [gui]
        tested-under: [view 1.2.8.3.1 on W2K]
        support: none
        license: none
        see-also: none
    ]
]
 
flash "Fetching image..."
read-thru/to http://www.rebol.com/view/demos/palms.jpg%palms.jpg

read-thru/to http://www.rebol.com/graphics/reb-logo.gif%rebo-logo.gif
read-thru/to http://www.rebol.com/view/bay.jpg%bay.jpg
pic: %palms.jpg
unview

customer: make object! [  ; this sets a default customer object in 
case the user does not push the samples in order
        name: "Rosetta Stone"
        date-of-birth: 14-March-1959        
]

stylize/master [text-note: txt maroon bold]         ; this sets a 
default for users who run the samples out of order
; polished is an image that is embedded in this script file
; so that no outside files need to be loaded.
; This technique is used in many of the REBOL samples

polished: load #{      
89504E470D0A1A0A0000000D49484452000000670000003808020000006FFB71
8C0000001374455874536F667477617265005245424F4C2F566965778FD91678
0000039B49444154789CCD9BD14E2B310C44F3FFFF521E2AC1C395E00589EF02
5DD8D226713C339EECBD928510A4893D3EF67AB76D7B7E79FEB2D73FAFBDBD1D
36FEEBAF7DBF70FEDABABD0D56F0E1B6E0B6FED7AE81050B2E4F97AF9FED6185
45022048CA2C6920361F1336580B35A63C4E4F12808D378124CE9C81880B186C
14175A1DE9C0C2A2E785B6B64CA62EF6C626330250A932064CB984F3358FA77F
BC7F8CD657685E5FB03415A34B9E3226C1484A1ACA6DB6974597699EFB6C2F4E
C44B92E17454A309F14F348DDD4D5B98195BB2AF6B7E4E545B57FEBAE0415DA0
43EFE62C70B196362285D74C35F0782ECF26A0FC8492E20EAAED6CC35ACE13FC
61646467C69D5715EA4F9D3725B1703BF45AB2768A9D5F59CA6E716E5A747CBF
23D6A7E418C2C53C6EB440FCE803B106E6D94C8AAF4B42694871B9FB237035C8
70E5A0200D4A7E4553E952A6F435BA247DD4B83A5DB18D26AA4D5E39E0CA4107
7B0F86C7F19EC685B5EFE57D28E02E411CC2238C0304B5065D26DCC1DA9A02FC
CE4EBD19A0C58D9BE039179086B6DB20519A1F5C8194071DBB115703B996FF37
4BE0F5AA269B18CFC9C6CC1FC3D5A0D6B62C6AEA112236F38195B88DF12F9C9B
B45B1C6C637B161E1D5BCF6D8807A0437366CB21D90462F2BD3E827C96CB0483
D67B99916407E99E69FD12F46A50C973856268A5DC345259837D8827E00FF890
452D0B5D0D38932F3C65B9614B4F720901B96565DCCBD7236B7C66B650868D08
9BFA26EBB36DCAA5E3B120339E5EBE3B468E6B68FB3041E79229047367ADDC14
F7B376F26B2722024A41998813A04CB91A475C11C9054536473CA3F7365C0D30
9E65A0BA6D5977CFEE030BD626B9E2E5DFE76E51AFF9CADA6308F899C76E756A
03D4796E80532E986D273B71CEA8D81672739E0F1B329F8E999D0D9D04080769
CABC1D21260C2BB8E43D2A9D70BE3A2207D66EA09E5BCAFFB742F9F0A0C37677
222CFE9B7C2865192B3FAC5988E0385747334BD8288041E0DFBF4F2AD44804B4
6DADAF2BE98C5D02458059B3571CA91481B09580A9D6E827B184DD3756D6BF7E
7376F81ED59E46633384296A9A4BA7D4E3B8CBD3E566F1948B754731E0EBC41B
246774BD7BBBCA612D8CA7CC85A7C1ED093B75721DCED1D7E279871668830AE7
B782F5E9FDE4918360C9F666A6F61647F2EDB342A2FA3F6E9B0C8AC2699B9B3F
53847BB992B5707FDE5B6D721EA3EB55E3D8190D8BD998923A68917BE3FEDD32
EE1BDCA216275C1CCFBD0A07F35A40A6CC05A1357E6BF512D26DC470BAC927A3
B0078A42DD22E10000000049454E44AE426082
}
content: {VID Usage - REBOL Visual Interfaces


===Updates

--01-Apr-2008


* Fixed oddity with last item on stylesheets which was locking up 
some versions of VIEW

---09-Jan-2004

* Fixed slider initialize. 

* Focus section was not parsed out. --- fixed

* Fixed some text errors for the parsing of ===


---07-Jan-2004

* Revived vid-usage.r 

* added more examples from the script library

* manage source as vid-usage.leo an outliner file

---12-August-2001

Added supply examples. See:

!List/Supply

!List With Supplied Data

!Supply List With Scroll

---13-August-2001

!Add Subpanel example ported by Anton

===Caveats

---Work In Progress


This is a work in progress. Whether the progress will continue depends 
on the feedback.

---All Rights Reserved


The work is based on the documentation of REBOL View provided by 
REBOL Technology and its mailing list.

All rights to this documentation remain the property of REBOL Technology.

---Plagiarized Examples


Things are shamelessly plagiarized.  There are many experts on the 
mailing list whose work is included here; most notably the examples 
from the REBOL documentation.

---Approach


The approach that this document uses is to use REBOL/View/VID to 
demonstrate its abilities and give a visual tutorial. To enable this 
some changes have been made to the core %easyvid.r program from Carl 
Sassenrath. A scoll bar was added to the right pane because it was 
just too difficult to constrain the examples to the screen real estate 
that was available.

---Order Order


The order of the items needs some work. The easyvid presentation 
approach today does not allow for the drilling down and expansion 
of an outline tree which is needed for a large amount of documentation.


The preferred approach is to put a multi-level tree for navigation 
purposes and then allow navigation up and down the tree. 


===To Do

* make this a true outline tree

* re-organize it better


* update as requested and as possible by suggestions on AltME's REBOL 
world under group EasyVID

* correct numerous flaws


* better scrolling implementation using the updates that have been 
used in other examples such as Didier's %delete-email.r


* allow clipping to clipboard like AltME does on a row for the source 
examples




===Introduction to VID

With REBOL/View it's easy and quick to create your own user
interfaces. The purpose of this tutorial is to teach you the
basic concepts or REBOL/View interfaces in about 20 minutes.

VID is REBOL's Visual Interface Dialect.  A dialect is an
extension of the REBOL language that makes it easier to express
or describe information, actions, or interfaces.  VID is a
dialect that provides a powerful method of describing user
interfaces.

VID is simple to learn and provides a smooth learning curve from
basic user interfaces to sophisticated distributed computing
applications.


---Creating VID Interfaces

VID interfaces are written in plain text. You can use any text
editor to create and edit your VID script. Save your script
as a text file, and run it with REBOL/View.

!Note: Using a word processor like Word or Wordpad is not
recommended because files are not normally saved as text.
If you use a word processor, be sure to save the output
file as text, not as a document (.doc) file.


Recommendation: Look at TextPad from http://www.textpad.com




===Minimal VID Example

Here is a minimal VID example.  It creates a window that
displays a short text message.  Only one line of code
is required:

    view layout [text "Hello REBOL World!"]

You can type this line at the REBOL console prompt, or save
it in a text file and run it with REBOL.  If you save it
as a file, the script will also need a REBOL header. The
header tells REBOL that the file contains a script. Here
is an example of the script file with a header:

    REBOL [Title: "Example VID Script"]

    view layout [text "VID Example!"]

You can also add buttons and other gadgets to the script. The
example below displays a text, list of files, and a button:

    view layout [
        h2 "File List:"
        text-list data read %.
        button "Great!"
    ]

!Click on the examples above to see how they will appear on your
screen.  Click on their close box to remove them.  All of the
examples that follow can be viewed this way.


===Window Management


The code that displays the examples also shows how to manage the 
number of windows that are open.


Look at the show-example block in the code near the end of this script.


The location of the example window is also managed here by keeping 
track of the co-ordinates for the sample. After the sample window 
is moved, the next use will open at the same location.



===Pre-loaded Images


For this script, the image which represented a Portable Network Graphic
definition of an image is held in the script and loaded.


For a small number of graphics, this can achieve some packaging and
performance benefits.


The image "polished" is used through the script to achieve the polished 
steel
look that is one the outer frame.

    backtile polished orange
    button 200x50 "Polished Steel Look" polished 


===Two Basic Functions

Two functions are used to create graphical user interfaces
in REBOL: VIEW and LAYOUT.

The LAYOUT function creates a set of graphical objects.  These
objects are called faces.  You describe faces with words and

values that are put into a block and passed to the LAYOUT function.

The VIEW function displays faces that were previously created by
LAYOUT. The example below shows how the result of
the LAYOUT function is passed to the VIEW function, and the
interface is displayed.

    view layout [
        text "Layout passes its result to View for display."
        button "Ok"
    ]

Click on the above example to view it.

!Note: the block provided to a layout is not normal REBOL code,
it is a dialect of REBOL.  Using a dialect makes it much easier
to express user interfaces.



===Styles

Styles describe faces.  The examples above use the text and
button styles to specify a text line and a button. REBOL has
40 predefined face styles. You can also create your own custom
styles.  Here are a few example styles:

    view layout [
        h1 "Style Examples"
        box brick 240x2
        vtext bold "There are 40 styles built into REBOL."
        button "Great"
        toggle "Press" "Down"
        rotary "Click" "Several" "Times"
        choice "Choose" "Multiple" "Items"
        text-list 120x80 "this is" "a list" "of text"
        across
        check
        radio radio
        led
        arrow
        below
        field "Text Entry"
    ]


The words like backdrop, banner, box, text, and button are styles.

===Facets

Facets let you modify a style.  For instance, you can change the
color, size, text, font, image, edge, background, special
effects, and many other facets of a style.

Facets follow the style name.  Here is an example that shows
how you modify the text style to be bold and navy blue:

    view layout [txt bold navy "Facets are easy to use."]

The words bold and navy are not styles.  They are facets that
modify a style. Facets can appear in any order so you don't
have to remember which goes first.  For example, the line
above could be written as:

    view layout [txt "Facets are easy to use." navy bold]

Many facets that can be specified.  Here is an example that
creates bold red text centered in a black box.

    view layout [txt 300 bold red black center "Red Text"]

You can create facets that produce special effects, such
as a gradient colored backdrop behind the text:

    view layout [
        vtext bold "Wild Thing" effect [gradient 200.0.0 0.0.200]
    ]

===Custom Styles

Custom styles are shortcuts that save time.  When you define a
custom style, the facets you need go into the new style.  This
reduces what you need to specify each time you use the style,
and it allows you to modify the look of your interface by
changing the style definitions.

For example, here is a layout that defines a style for red
buttons.  The style word defines the new style, followed by
the old style name and its facets.

    view layout [
        style red-btn button red
        text "Testing red button style:"
        red-btn "Test"
        red-btn "Red"
    ]

So, if you wanted to create a text style for big, bold,
underlined, yellow, typewriter text:

    view layout [
        style yell tt 220 bold underline yellow font-size 16
        yell "Hello"
        yell "This is big old text."
        yell "Goodbye"
    ]


===Master Stylesheet 

REBOL holds its styles in a master stylesheet. When you are
sure that you want to share them without having to add the
style sheet line then do it as follows:

First add the style to the master sheet:

    button 200x50 "Define text-note as maroon bold text" [stylize/master 
    [
        text-note: txt maroon bold      
    ]]

    button 200x50 "Define text-note as white italic text" [stylize/master 
    [
        text-note: txt white italic     
    ]]

Then invoke it:

    view layout [
        across
        size 200x200

        return text-note "This shows a master stylesheet style in use." 

        return text-note "This shows another usage of the same style."

        return text-note "If you want to see the other style displayed, click 
        the Add Style section again and then use the other button"
    ]
    
    
===Note About Examples

!From this point forward, all examples will assume that
the view and layout functions are provided.  Only the layout
block contents will be shown.  To use these examples in your
scripts, you will need to put them in a layout block, as was
shown earlier.

For example, code that is written as:

    view layout [button red "Test it"]

will now appear as:

    button red "Test it"


===Face Sizes

The size of a face depends on its style.  Most styles, such as
buttons, toggles, boxes, checks, text-lists, and fields, have a
convenient default size.  Here are some examples.

    button "Button"
    toggle "Toggle"
    box blue
    field
    text-list

If no size is given, text will automatically compute its size,
and images will use whatever their source size is:

    text "Short text line"
    text "This is a much longer line of text than that above."
    image %palms.jpg

You can change the size of any face by providing a size facet.
The size can be an integer or a pair.  An integer specifies
the width of the face.  A pair specifies both width and height.
Images will be stretched to fit the size.

    button 200 "Big Button"
    button 200x100 "Huge Button"
    image %palms.jpg 50x50
    image %palms.jpg 150x50

===Color Facets

Most styles have a default color.  For example the body of
buttons will default to a teal color.  To modify the color of
a face, provide a color facet:

    button blue "Blue Button"
    h2 red "Red Heading"
    image %palms.jpg orange

Colors can also be specifed as tuples. Each tuple contains three
numbers: the red, green, and blue components of the color. Each
component can range from 0 to 255. For example:

    button 200.0.200 "Red + Blue = Magenta" 200
    image %palms.jpg 0.200.200 "Green + Blue"

Some face styles also allow more than one color.  The effect of
the color depends on the style.  For text styles the first color
will be used for the text and the second color for the background
of the text:

    txt "Yellow on red background" yellow red
    banner "White on Navy Blue" white navy

For other styles, the body of the face is the first color, and
the second color will be used as its alternate.

    button "Multicolor" olive red
    toggle "Multicolor" blue orange
===Layout Commands


To drop user interface elements on the canvas according to VIDs 
directional layout controls 

---Across

You are placing elements in a row orientation
    
    across 
    return button "A" button "B" button "C"
    return button "D" button "E" button "F"
    

---Below

You are placing elements in a column orientation

    below 
    return button "A" button "B" button "C"
    return button "D" button "E" button "F"

---Mix

You can mix the directional controls 

    across 
    return button "A" button "B" 
    below button "C" 
    across button "D" button "E" button "F"


---Padding


The pad keyword creates extra padding between styles. It uses a pair 
or integer value. When it is an integer, spacing is created either 
horizontally (across) or vertically (below). When it is a pair, the 
spacing will be created both horizontal and vertically. The following 
example illustrates both uses. First, the buttons "one" and "two" 
are padded with an integer representing 40 pixels in one direction. 
Then the buttons "three" and "four" are padded with a pair representing 
40x40 pixels. 

    across 
    button "one" pad 40 button "two" return 
    button "three" pad 40x40 button "four" 


Padding can be negative.
    
        backtile polished orange
        pad 200x200 button "A"
        pad -100x-100 button "B"
        
---Guide

A guide is a virtual alignment control

      title "Buttons Without A Guide" 
    button "one"   button "two"  return 
    button "three" button "four" return 
    button" five" button "six" 

With an implicit guide location

    title "Buttons With An Implicit Guide Location" 
    guide 
    button "one"   button "two"  return 
    button "three" button "four" return 
    button" five" button "six" 

With an explicit guide location

    across title "Buttons With An Explicit Guide Location"
    guide 55x100 
    button "one"   button "two"  return 
    button "three" button "four" return 
    button" five" button "six" 
    
===Tabstops

Tabs can be used for alignment.

---Across

    tabs 200 ; sets tabs every 200 pixels   
    across button 20 "A" tab button 20 "B" tab button 20 "C" 
    tabs 100 ; sets tabs every 100 pixels   
    return button 20 "D" tab button 20 "E" tab button 20 "F"
    
---Below

    tabs 200 ; sets tabs every 200 pixels   
    below button 20 "A" tab button 20 "B" tab button 20 "C" 
    tabs 100 ; sets tabs every 100 pixels   
    return button 20 "D" tab button 20 "E" tab button 20 "F"

---Explicit Settings

Tabstops can be set at explicit values 

    tabs [100 124  166 212 300]

    across tab button 20 "A" tab button 20 "B" tab button 20 "C" tab 
    button 20 "D"
    
===Color Facets

Most styles have a default color.  For example the body of
buttons will default to a teal color.  To modify the color of
a face, provide a color facet:

    button 200 blue "Blue Button"
    h2 red "Red Heading"
    image polished orange

Colors can also be specifed as tuples. Each tuple contains three
numbers: the red, green, and blue components of the color. Each
component can range from 0 to 255. For example:

    button 200.0.200 "Red + Blue = Magenta" 200
    image polished 0.200.200 "Green + Blue"


Some face styles also allow more than one color.  The effect of the 
color depends on the style.  For text styles the first color will 
be used for the text and the second color for the background of the 
text:

    txt "Yellow on red background" yellow red
    title "White on Navy Blue" white navy


For other styles, the body of the face is the first color, and the 
second color will be used as its alternate.

    button 200 "Multicolor" olive red
    toggle 200 "Multicolor" blue orange


From the mailing list, there was a problem reported in changing button 
color:

    view layout [
        b: button "New color" [
            b/color: random 255.255.255 
            show b
        ]
    ]
    

And the answer was that the gradient of the color was preventing 
this change from working:


    style color-changing-button button 0.0.0        ; new style overwrites 
    gradient effect
    b: color-changing-button "New color" [
        b/color: random 255.255.255 
        show b
    ]

===Text Facets


Most faces will accept text to be displayed.  Even graphical faces 
can display text.  For example, the box and image faces will display 
text if it is provided:

    box blue "Box Face"
    image polished "Image Face"


Most button faces will accept more than one text string. The strings 
will be shown as alternates as the face is selected.

    button 200 "Up" "Down"
    toggle 200 "Off" "On"
    rotary 200 "Red" "Green" "Blue" "Yellow"
    choice 200 "Monday" "Tuesday" "Wednesday" "Thursday" "Friday"

    text-list 200 "Monday" "Tuesday" "Wednesday" "Thursday" "Friday"


When other datatypes need to be displayed as text, use the form function 
to convert them first:

    button 250 form now
    field form first read %.
    
===Normal Text Style


Normal text is light on dark and can include a number of facets to 
set the font, style, color, shadow, spacing, tabbing, and other attributes.

    text "Normal"
    text "Bold" bold
    text "Italic" italic
    text "Underline" underline
    text "Bold italic underline" bold italic underline
    text "Big" font-size 32
    text "Serif style text" font-name font-serif
    text "Spaced text" font [space: 5x0]

Text also includes these predefined styles:

    title "Title" 200
    vh1 "vh1"
    vh2 "vh2"
    vh3 "vh3"
    vh4 "vh4"
    label "Label"
    
    
===Document Text Style


Document text is dark on light and can also include a number of facets 
to set the font, style, color, shadow, spacing, tabbing, and other 
attributes.

    txt "Normal"
    txt "Bold" bold
    txt "Italic" italic
    txt "Underline" underline
    txt "Bold italic underline" bold italic underline
    txt "Big" font-size 32
    txt "Serif style text" font-name font-serif
    txt "Spaced text" font [space: 5x0]

Document text also includes these predefined styles:

    title "Centered title" 200
    h1 "Heading 1"
    h2 "Heading 2"
    h3 "Heading 3"
    h4 "Heading 4"
    tt "Typewriter text"
===Text Entry Fields


Text input fields accept text until the enter or tab key is pressed. 
 A text input field can be created with:

    field

To make the field larger or smaller, provide a width:

    field 30
    field 300

Fields will scroll when necessary.


Larger amounts of text can be entered in an area.  Areas also accept 
an enter key and will break lines.

    area

You can also specify the area size:

    area 160x200


To force the text in an area to wrap rather than scroll horizontally, 
provide the wrap option:

    area wrap
===Text Setting


To set the value of a text field under program control, use /text: 
e.g.

    across backtile polished
    return t1: txt      200 "This is some original text"
    return f1: field    200 "Some field text"   
    return a1: area  {Some original area text.} wrap 200x80
    return button 200 "Change Text" [
        t1/text: "Some different text" 
        f1/text: "Some new field text"

        a1/text: {Some wrapping text in the^/ area field to^/ show that this^/ 
        is supported}
        show [t1 f1 a1]
    ]
    
===Text Lists

Text lists are easy to create.  Here is an example.

    text-list "Eureka" "Ukiah" "Mendocino"

You can also provide it as a block:

    text-list data ["Eureka" "Ukiah" "Mendocino"]


Almost any type of block can be provided. Here is a list of all the 
files in your current directory:

    text-list data read %.

Here is a list of all the words REBOL has scanned:

    text-list data first system/words
===Scrolling Text List


A style to allow maintenance of lists from Brett Handley on the REBOL 
list:

        style updatable-text-list text-list
        with [
            update-slider: does [
                sld/redrag lc / max 1 length? head lines
            ]
        ]

        tl: updatable-text-list 300x100 data copy system/locale/months
        button  300x20 "Delete first entry on the list" [
            remove tl/data tl/update-slider show tl
        ]
        button 300x20 "Append the 'now' timestamp to list" [
            append tl/data mold now tl/update-slider show tl
        ]
===Text List Picked Values

    list-of-letters: text-list "a" "b" "c" "d" "e" 
    button 200  "Pick Item 3" [
        clear list-of-letters/picked 

        append list-of-letters/picked pick list-of-letters/data 3 
        show list-of-letters
    ]
===Images

By default an image will be scaled to fit within a face.

    image 60x60 polished
    image polished red

Images can be framed in a number of ways:

    image 100x100 polished frame blue 5x5
    image 100x100 polished bevel
    image 100x100 polished ibevel 6x6

Most other faces can accept an image as well as text:

    box 100x100 polished
    button "Button" polished purple
    toggle "Toggle" polished blue red
    field bold  "This is a field." polished effect [emboss tile]

    field bold "This is another field." polished effect [brighten 100]

The image can be provided as a filename, URL, or image data.



===Backdrops

A backdrop can be a color, an effect, an image, or a combination
of the three.  For example a backdrop color would be written as:

    backdrop navy
    title "Color Backdrop" gold

To create a backdrop effect provide it on the line:

    backdrop effect [gradient 1x1 0.0.100 100.0.0]
    title "Gradient Backdrop" gold

A backdrop image can be a file, URL, or image data:

    backdrop polished
    title "Image Backdrop" red

The backdrop image can be colorized:

    size 400x500 
    backdrop polished blue
    title "Blue Image Backdrop"

The image can include an effect:

    backdrop polished effect [fit gradcol 1x1 100.0.0 0.0.250]
    title "Gradient Image Backdrop"
    
===Backtile

To make a backdrop use a tile effect there are two options:

    backdrop polished effect [tile]
    banner "This shows a backdrop with a tile effect"
    
or

    backtile polished
    banner "This demonstrates backtile"


Note the difference between:

    size 400x500 
    backdrop polished
    banner "Here one image is stretched to cover the canvas"

and

    size 400x500 
    backtile polished
    banner "Here one image is repeated to cover the canvas"
    
===Effect Facets


A range of effects are supported for faces.  All of these effects 
are performed directly on the face when it is rendered. Here are 
examples of a few possible effects in top to bottom then left to 
right order:

    style polished-steel image 80x60 polished 
    polished-steel effect [flip 1x1]
    polished-steel effect [rotate 90]
    polished-steel effect [reflect 1x1]
    polished-steel effect [crop 0x50 120x60 fit]
    polished-steel effect [grayscale]
    polished-steel effect [invert]
    polished-steel effect [difference 200.0.0]
    polished-steel effect [tint 80]
    return
    polished-steel effect [contrast 50]
    polished-steel effect [brighten 50]
    polished-steel effect [sharpen]
    polished-steel effect [blur]
    polished-steel effect [colorize 204.0.0]
    polished-steel effect [gradcol 1x1 150.0.0 0.0.150]
    polished-steel effect [gradmul 0x1 0.100.0]
    polished-steel effect [grayscale emboss]


Effects can be used in combination to create other interesting results. 
 However, keep in mind that the computations are performed in real 
time.  If complex combinations are required, a temporary image should 
be created with the to-image function.


===Actions


An action can be associated with almost any face. To do so, follow 
the face style with a block:

    button "Test" [alert "test"]


The block is used as the body of a function that is passed the face 
and the current value (if the face has one).  For example:

    toggle "Toggle" [alert form value] 
    rotary "A" "B" "C" [alert form value]
    text "Click Here" [alert  face/text]

If a second block is provide, it is used for the alternate
actions (right key):


    button "Click Here" [view/new layout [txt "action"]] [view/new layout 
    [txt "alt-action"]]


Use variables to modify the contents or state of other faces. For 
example, the slider will update the progress bar:

    slider 200x16 [p1/data: value show p1]
    p1: progress

!More action on actions needed...

===Show


After the state is changed for a user interface element, it must 
be re-drawn to be reflected on the user interface canvas.

Accomplish this with the show message.

    backtile polished
    across 
    toggle "Toggle State"  
            [   cybernetics?/data: not cybernetics?/data
                show cybernetics?]  

    return  label "Are you interested in cybernetics?" cybernetics?: 
    check 

One show command can be used for multiple user interface elements

        backtile polished orange
        across
        b1: check label "Red" return
        b2: check label "Green" return

        button 200 "Change State But No Refresh" [b1/data: not b1/data b2/data: 
        not b2/data] return
        button "Show" [show [b1 b2]]
===Hide

A user interface element can also be hidden.

        backtile polished orange
        across
        c1: check 
        hide-button: button "Hide" [hide c1] return
        show-button: button "Show" [show [c1 d2]]


The show-button action tries to show a user interface element 'd2' 
that does not exist.
REBOL/View ignores these.
===Invisible Faces


To make a button invisible when the view is opened, you can define 
an invisible button style

This approach sets the show? value to false when the user interface 
element is initialized.

    across

    style invisible-button button with [append init [show?: false]]
    late-shower: invisible-button "I'm Here" return
    return button 200 "Show Invisible Button" [show late-shower]
    
This works for the other visible user interface element.

===Focus

A user interface element can programmatically be given the focus.

    across backtile polished 
    button 200 "Set focus to Phone Field" [focus f2] 
    return label "Name: "   f1: field 100 
    return label "Phone: " f2: field 100 

    return button 200 "Remove focus from Phone Field" [unfocus f2] 
    return button 200 "Hide the Phone Field" [hide f2] return

---Focus Defect


!Note that the tab function shows a hidden field. I have assumed 
that this is a defect. If a field is hidden, the tab button should 
not make it visible. This has been previously sent to feedback.

===Radio Buttons


A radio button is used to make a choice between mutually exclusive 
values. Your preferred programming language is REBOL or C++ or PL/1 
or APL but it is only one of those.

            across backtile polished

            radio of 'programming-language pad 0x-4 label "REBOL" return

            radio of 'programming-language pad 0x-4 label "C++" return

            radio of 'programming-language pad 0x-4 label "PL/1" return

            radio of 'programming-language pad 0x-4 label "APL" return


To mix two groups of radio buttons on one screen, associate them 
with their groups using the "of 'word". In the above, the grouping 
is 'programming-language.

            across backtile polished

            radio of 'programming-language pad 0x-4 label "Language: REBOL" return

            radio of 'programming-language pad 0x-4 label "Language: C++" return

            radio of 'editor pad 0x-4 label "Editor: TextPad" return

            radio of 'editor pad 0x-4 label "Editor: Notepad" return


The padding in the above is needed to keep the label aligned with 
the radio button.

            across backtile polished orange

            radio of 'programming-language pad 0x-4 label "REBOL" return

            radio of 'programming-language          label "APL" return
            
            
            
            

===Radio Button Settings


A radio button is not very useful unless you can find out what its 
setting is and change that setting under program control.


            across backtile polished orange

            rebol-radio:    radio of 'programming-language  [programming-language: 
            'rebol] pad 0x-4 label "REBOL" return

            apl-radio:      radio of 'programming-language  [programming-language: 
            'apl]         label "APL" return
            button 200 "Toggle radio button" [

                    apl-radio/data: not rebol-radio/data: not rebol-radio/data
                    show [rebol-radio apl-radio]
            ]
            
===Check Box

---Purpose


A check box is used to allow user interface choices where the choices 
are not mutually exclusive.

        across backtile polished orange
        c1: check label "Likes animals" return
        c2: check label "Like Monkees" return
        c3: check label "Like The Animals"
        
---State


A check box is not much good if you can't get and set its state (on 
or off).

        across backtile polished orange
        c1: check label "Likes animals" return
        c2: check label "Like Monkees" return
        
    button "Set State" [
        c1/data: true 
        show c1
        c2/data: false
        show c2
    ]
 
 
===Sensor

---Purpose


A sensor is an invisible user interface element. Using a sensor only 
makes sense in a few instances. 


If you want a keycode action where there is no visible user interface 
element to link the action to then a sensor can be used.


This sensor code adds an Escape or Back or Enter action that will 
close the window. 

    sensor 1x1 keycode [#"^M" #" " #"^(back)" #"^(ESC)"] [unview]


Or if you want to make portions of an image 'hot' instead of putting 
buttons on top of the image, then a sensor will achieve this.

        across backtile polished orange

        txt "Click on the upper left section of the gray image to invoke 
        the sensor action"

        return animage: image 100x100 polished      ; here the image is just 
        the polished area

        at animage/offset sensor 50x50 [alert "You pushed over the sensor"]
        
===Displaying Script Values


If the script has a standard format headings, including custom ones, 
these can be used in the application by picking them from the system/script/header.

        backtile polished
        across banner "About"

        return text font-size 16    rejoin ["Title: "           form system/script/header/title]

        return text font-size 16  rejoin ["Originator: "    form system/script/header/author] 
              

        return text font-size 16  rejoin ["Modifier: "      form system/script/header/modifier]

        return text font-size 16  rejoin ["Version: "       form system/script/header/version]

        return text font-size 16  rejoin ["Updated: "       form system/script/header/date]
        return button "OK" [unview] 

===Toggle


A toggle button represents boolean state - either on or off. The 
button stays down until toggled again.
Colors and text can be paired for "on" and "off" state.

    toggle "Up" "Down" red blue

To set the state via program control, use:

        across backtile polished
        return t1: toggle "Up" "Down" red blue
        return button polished 204.0.0 100 "Toggle State" [
            t1/state: not t1/state
            show t1
        ]


===Rotary Buttons


Rotary buttons are a different sort of user interface device. They 
can cause some challenges because the state is what's showing so 
you have to blindly "toggle" to get to a state that you want.  But 
for quick and easy uses where the user is familiar with the options, 
they can be handy.  If you plan to use them for a long list of items 
such as shown  below, they might give you some usability concerns.


---Example

        across backtile polished
        rotary data ["First" "Second" "Third"]

---Setting State

        across backtile polished

        return r1: rotary data (my-options: ["First" "Second" "Third"])
        return button 200 "Change Rotary State" [
            r1/data: next r1/data
            if tail? r1/data [r1/data: head r1/data]
            show r1
        ]

---Example - Usability For Unfamiliar List Contents


The rotary button demonstrated here contains some information unfamiliar 
to most (Saturn's satellites). Use it to to set the state so that 
"Calypso" is set. Doable but without knowing the order each re-paint 
has to be checked to ensure that it is not "Calypso" before clicking 
again.  If you do click past the choice that you want, there is no 
back function so you have to cycle through again.

        return rotary data [

            "Pan" "Atlas" "Prometheus" "Pandora" "Epimetheus" "Janus" "Mimas" 
            "Enceladus" "Tethys" "Telesto" "Calypso" "Dione" "Helene" "Rhea" 
            "Titan" "Hyperion" "Iapetus" "Phoebe"        
        ]


===Arrows


REBOL/View supports arrows as simple user interface elements. Actions 
can be associated with them.



---Arrowheads And Actions

By default, the arrow is 20x20

    across size 200x100 
    backtile polished
    at 50x50 arrow left 
        [alert "You pressed the left arrow"] 

        [alert "You pushed the alternate button on the left arrow"] 
    at 70x30 arrow up
    at 90x50 arrow right [alert "You pressed the right arrow"]
    at 70x70 arrow down
    
---Very Sharp Arrows


And with a little work the arrows and boxes can be merged to look 
sharper. Here is a "sharp at both ends" arrow from the block diagram 
script by Carl:

    origin 0
    backcolor white
    at 0x0 box 40x40 white effect [arrow rotate 270]
    at 110x0 box 40x40 white effect [arrow rotate 90]
    at 24x10 box black 100x20
    
---Arrow Blend

So that shows you how to make an arrow blend into your background

    size 100x100
    across backdrop gray
    at 50x50 box 40x40 gray effect [arrow rotate 90]
    at 40x67 box 25x5 black
    
===LED

LEDs would be used to display state (on or off).  

Clicking the LED toggles its state and changes its color.

LEDs do not support alternate mouse button actions.

    across banner "Light Emitting Diode"
    return 

    l1: led 10x10 [alert "LED left mouse action"] label "Alert status"

    l2: led 10x10 [alert "LED left mouse action"] label "Network status"
    return button "Change state" [
        l1/data: not l1/data
        l2/data: not l2/data 
        show [l1 l2]        
    ]
    
===Box


---Boxing

Draw boxes of any heigth and width with the box style

    box "Large Box" 200x400 polished orange
    
---Boxes As Lines


If you make the box narrow enough or short enough it is a line (or 
a dot).

    across size 300x300 backtile polished
    at 50x0 box 3x100 gold
    at 0x50 b1: box 100x3 gold
    at 10x10 box 5x5 red
    
---Boxes Can Grow

    across size 300x300 backtile polished
    at 150x0 b1: box 100x3 gold
    return pad 0x100 button "Grow Down" [
        for i 3 300 1 [
            b1/size/y: 1 + b1/size/y
            wait 00:00:00.01
            show b1
        ]
    ]

    return pad 0x100 button "Back Up" [
        for i 300 3 -1 [
            b1/size/y: b1/size/y - 1
            wait 00:00:00.01
            show b1
        ]
    ]

You might even find a use for it.


---Grid Effect

Not sure of the use for this yet but here is what you can do:


 return box "Grid Lock" with [effect: [grid 20x20 8x8 4x3]] white 
 300x200
 

 return box "Grid Lock" with [effect: [grid 20x20 5x5 3x3]] white 
 - 80 300x200
 
===Frame

Earlier versions of REBOL VID supported frames in layouts such as
view layout [frame "This is the Bay" %bay.jpg]

These are no longer valid.

But frames can be put around some user interface devices:

    image 100x100 polished frame red
    
===List


A list is an iterated sub layout and takes a layout block that uses 
the Visual Interface Dialect. The styles in the layout will be repeated 
until there is no more room to fit them within the list dimensions.


---Why


A face can be iterated to create a number of virtual faces. For instance, 
when displaying a list of ten buttons, each of the buttons does not 
need to be created as a separate object. If the buttons only differ 
by a few facets (such as position, text, and action taken on selection), 
a model face can be created and iterated for its other position. 
This is useful when creating scrolling lists of files and other data 
sets that share the same appearance. 

---Supply


Supply provides the data to the list for an iterated face.

    do [cnt: 0
        list-collection: [aqua sky water]    
    ]
    backtile polished orange
    across
    list-displayed: list 100x72 [
        origin 0 space 0x0 across
        color-field: txt bold 80x24
    ] supply [
            if none? one-color: pick list-collection count [exit]
            face/text: do pick [one-color] index

    ]

    return txt gold 180 "OK ... but not too useful"


---Supply Columns

Maybe adding some more columns would be better.

Here I'll add a column of buttons that display the color name
and a column of text strings in italic.

    do [
        cnt: 0
        list-collection: [aqua sky water gold silver coffee]    
    ]
    backtile polished orange
        across
        list-displayed: list 300x200 [
            origin 0 space 0x0 across
            color-field: txt bold 80x24
            color-button: button 80x24
            pad 5x1
            txt 100 italic
        ] supply [

                if none? one-color: pick list-collection count [exit]
                face/text: do pick [
                    [one-color]
                    [to-string one-color]
                    [rejoin ["  " to-string one-color]]
                    ] index 
    ]
    

    return txt gold 300 {A bit more interesting but the last row repeats 
    to fill the list size. Some of the other VID components will automatically 
    stretch to fit the size needed (such as this txt field) but the list 
    does not behave that way.  You have to make the list size fit its 
    data or make it smaller and add a vertical scroll capability. That 
    is shown a little later on.}

===List With Supplied Data

This example is to show adding action to the list
and adds a horizontal line between the rows.


    do [
        cnt: 0
        list-collection: [aqua sky water gold silver coffee]    
    ]
    backtile polished orange
        across

        list-displayed: list water edge [size: 6x6 color: silver]  350x96 
        [  
            origin 0 space 0x0 across

            color-field: txt 60 [alert rejoin ["You pressed the " face/text " 
            text field"]]
            pad 45x0            

            color-button: button 80 [alert rejoin ["You pressed the " face/text 
            " button"]]
            pad 5x0
            txt 120 italic

            return box 350x1 white      ; this causes a horizontal line to appear 
            between each row

        ] supply [

                if none? one-color: pick list-collection count [exit]
                face/text: do pick [
                    [one-color]
                    [to-string one-color]
                    [rejoin ["  " to-string one-color]]
                    ] index 
    ]


===Supply List With Scroll

This example shows a supplied list with a scroll capability.
More colors are added to demonstrate scrolling.

Note that this is a verbose list of code where I added comments
for my understanding of how the scroll was linked to the list.
The same effect can be accomplished with fewer lines of code.
    

    do [    ; first this do block creates the data definitions needed.
        slider-position-clicked:  0
        count: 0    
        x: 450
        y: 300

        row-y: 16           ; the row height includes the data plus any separator 
        lines 

        list-size: to-pair reduce [x y]     ; this is the size of the display 
        list  
        separator-size: to-pair reduce [x 1]
        slider-size: to-pair reduce [24 y ]
        list-collection: [

            aqua           bar-color   base-color     beige         black    
                  blue           brick          brown      

        button-color   coal        coffee         crimson       cyan     
              forest         gold           gray       

        green          ivory       khaki          leaf          linen    
              magenta        main-color     maroon     

        mint           navy        oldrab         olive         orange   
              over-color     papaya         pewter         

            pink           purple      rebolor        red           sienna   
                  silver         sky            snow

        tan            teal        violet         water         wheat    
              white          yellow        
        ]    
        
        supply-style: stylize [

                button-fixed: button left coal to-pair reduce [80 row-y]        ; 
                these keep the row elements the same height

                text-fixed: txt to-pair reduce [160 row-y]          
        ]
        
        data-size: length? list-collection  
    ]
    

    backtile polished orange                        ; this section layouts 
    out the list
        
        across

        list-position: at                                   ; the position 
        is captured here in order to later put the slider beside it
        list-displayed: list linen 
            edge [size: 6x6 color: tan]  list-size [  
            origin 0 space 0x0 across
            styles supply-style

            text-fixed [alert rejoin ["You pressed the " face/text " text field"]]

            button-fixed [alert rejoin ["You pressed the " face/text " button"]]
            pad 5x0 

            text-fixed 80 italic [alert rejoin ["You pressed the italic " face/text 
            " text field"]]

            return box separator-size gray      ; this causes a horizontal line 
            to appear between each row

        ] supply [
                count: count + slider-position-clicked  

                if none? one-color: pick list-collection count [exit]
        face/text: 
            either count > (1 + data-size) 
            [""]
            [
                             do pick [

                                    [one-color]                                                      
                                           ; this is supplied to the first txt field (text-fixed)

                                    [to-string one-color]                                           ; 
                                    this is supplied to the button (button-fixed)

                                    [rejoin ["  " to-string one-color " "]]     ; this value is supplied 
                                    to the last text-fixed field
                            ] index 
                    ]
        ]


        ; now add a slider to the side of the list



        at list-position + (list-size * 1x0)                             
           ; this finds the top right border of the list widget
        vertical-slider: slider slider-size to-integer y / row-y
        [

                    slider-position-clicked: vertical-slider/data   ; the slider has 
                    to be bound to the size of the list                

                        * ((1 + data-size) - ((y / (1 + row-y))))       ; including the row 
                        height
                    if slider-position-clicked <> count [
                        count: slider-position-clicked 
                        show list-displayed
                    ]
                ]


===Slider


A slider is interactive user interface element. The data of a slider 
varies from 0 to 1.

    backtile polished   orange across
    slider-1: slider 200x40 
    return button 200 "Move first slider to 50%" [
        slider-1/data: .5 
        show slider-1
    ]

    return txt 200 "The second slider in this example is initialized 
    to the 80% mark."  
    return slider 200x40 with [append init [data: .8]]
    
===Progress Indicator


The progress-1 face in this example is a progress indicator. Because 
it is only displaying information, it is non-interactive i.e. you 
can not change its value by dragging its edges.  The alternate button 
is not supported on a progress indicator.

    backtile polished   orange across
    slider 200x40 [
        progress-1/data: value 
        field-1/text: join (to-integer (100 * value)) " %"
        show [progress-1 field-1]
    ] 
    return progress-1: progress
    return field-1: field
===Panels 


Panels are used to create sub-panes that can be more easily managed 
by grouping

the user interface devices on a panel. The first example below shows 
how to use panels for layout alignment. By creating a panel definition, 
all of the components defined within it are aligned relative to its 
origin.

    across backtile polished brick
    tabs 50
    return panel-1: panel 250x120 [
        backtile polished
        across
        return button water 200 "Button A"
        return button aqua  200 "Button B"
        return button sky   200 "Button C" 
    ]


    at panel-1/offset + panel-1/size panel 60x90 [  ; start at the bottom 
    right corner of panel-1
        backtile polished
        across
        return button tan       20 "1"
        return button coffee    20 "2"
    ]
    
---Multiple SubPanels example


This example from the REBOL html documentation shows how to easily 
hide and show sections of a user interface by displaying them on 
the face area of a box.  


        do [                                ; define two panels
            panel1: layout [
                    origin 8x8
                    h2 "Panel 1"
                    field "Field 1"
                    field "Field 2"
                    button "The Answer" [alert "I know nothing."]
            ]


            panel2: layout [
                origin 8x8
                    h2 "Panel 2"
                    across
                    txt "X:"
                    slider 150x16
                    return
                    txt "Y:"
                    slider 150x16
                    return
                    check [panel2/color: maroon  show panel2]
                    txt "Don't click this"
                    return 
                    check [panel2/color: silver  show panel2]
                    txt "Click this" 
                    return
                ]

                panel1/offset: 0x0
                panel2/offset: 0x0
        ]


        vh2 "Subpanel Examples"     ; now demonstrate panel use
        guide
        pad 20
        button "Panel 1" [panels/pane: panel1  show panels]
        button "Panel 2" [panels/pane: panel2  show panels]
        button "Quit" [unview]
        return
        box 2x140 maroon
        return
        panels: box 220x140
        do [panels/pane: panel1]
        
===Simple Default Style Override


The style's default look can be overriden easily with one line of 
code. 


For example, to make the default button size 200x200 with a water 
color, use

    style button button 200x200 water
    button "Big Blue Button" [unview]

To make the toggle some different default colors:

    style toggle toggle crimson sky
    toggle "Up" "Down"


Note that these stay in effect until they are overridden so if you 
use the default values, exercise some care unless you meant to do 
that. 



===Image Maker


An option used by Carl in some of his programs is to let View create 
specific icons so that you have portability and more control of look 
of the image then if you referenced an external file such as gif 
that was a bullet display. Here's how to do that:

    do [    
        make-image: func [xy wh eff] [
            eff: layout [
                size 20x20 at xy
                box wh effect eff
            ]
        eff/color: rebolor
        to-image eff
        ]


        dot: make-image 6x5 9x9 [gradient 1x1 255.0.0 0.0.0 oval key 0.0.0]

        dot-big: make-image 8x7 12x12 [gradient 1x1 255.0.0 0.0.0 oval key 
        0.0.0]
        arr: make-image 3x3 14x14 [arrow 0.0.127 rotate 90]
        ard: make-image 3x3 14x14 [arrow 0.0.127 rotate 180]    

    ]   ; end of "do" - it is needed here because easyvid approach is 
    expecting vid dialect commands

    banner "Presentation Points"
    size 400x300 across

    style label label gold     ; make a label's text be a different color 
    than the default
    return image dot label "This is bullet point number 1" 
    return image dot label "This is bullet point number 2"
    return image arr label "This is arrow point number 1"

    return image ard label "This is an arrow making a different point"

    return image dot-big pad 0x4 area 300x80 wrap "And because these 
    arrows and dots are images, action can be added to them to make them 
    'hot' with mouse actions including 'over'."
    
===Needs Some Work

!More to come.  These still need to be covered in this
tutorial:

    text-list data [
        icon
    ]
    
===Digital Clock

    origin 0
    banner "00:00:00" rate 1 effect [gradient 0x1 0.0.150 0.0.50]

        feel [engage: func [face act evt] [face/text: now/time  show face]]


 


===REBOL Logo

 image %rebo-logo.gif [unview]

===Paint Drops

REBOL one liner by Vincent Ecuyer


 b: box rate 9 effect[draw[pen(random snow)circle(random 99x99)2]blur]box 
 1x1 rate 9 effect[draw[(b/image: to-image b)]]
 
===eMailer

One line emailer by Doc Kimbel

Assumes you have set up your email in set-user



 e: field "Email" s: field "Subject" m: area "Body" btn "Send"[send/subject 
 to-email e/text m/text s/text alert "ok"]
 
===Hello World

 text "Hello World!" button "Close" [unview]
===Three Buttons

 button "Yes" button "Maybe" button "No"

===View Web Text

 text 800x600 read http://www.rebol.com
 
===View Image

 image %palms.jpg
 
===View Image and File Name


Here a do block is used to initialize the file variable within the 
layout code.

 do [file: %palms.jpg]
 image file  text form file
 
 
===View Image behind File Name


Here a do block is used to initialize the file variable within the 
layout code.

 do [file: %palms.jpg]

 image file form file
 
 
===Buttons From Images

    backdrop 40.70.140
    stat: text bold "Click a Button" 100x20 240.140.40 center
    button "Bay Test"  %bay.jpg 100x100 [
        stat/text: "Upper" 
        show stat
    ]
    button "Blue Test" %bay.jpg 100x100 10.30.180 [
        stat/text: "Lower" 
        show stat
    ]
===View List


 list blue 320x200 [across text white 200 text white 100] data [
    ["John" 100] 
    ["Joe" 200] 
    ["Martin" 300]
 ]
===Movie Credits



    backdrop %bay.jpg effect [fit]

    text center bold 240x30 "REBOL, The Movie" yellow font [size: 16]
    credits: text {

 Edit This File 

 To Add Your Own Credits 
 

 It is very simple to do. 

 Only takes a minute. 

 Only REBOL Makes It Possible...

 } white bold center 240x180 rate 30 para [origin: 0x+100]
        feel [engage: func [f a e] [

            if a = 'time [f/para/origin: f/para/origin - 0x1 show f]
        ]
    ]



===Fire Demo

    box 150x150 with [
        edge: none
        img: image: make image! 150x150
        rate: 20
        text: "FIREBOLEK"
        font: make font [size: 24 color: 255.125.0]

        basic: [draw [image make pair! reduce [(random 3)  - 2 -1] img]]
        effects: reduce [
            append copy basic [blur luma -10]
            append copy basic [sharpen luma -10 blur]
            append copy basic [contrast 10 blur luma -5]        
        ]
        effect: first effects
        feel: make feel [
            engage: func [f a e][
                switch a [

                    down [f/effects: next f/effects if tail? f/effects [f/effects: head 
                    f/effects] f/effect: first f/effects show f]

                    time [show f repeat i f/size/x - 4 [poke f/image (f/size/x * f/size/y) 
                    - i - 2 (random 255.0.0 + random 0.127.0) * 3] f/img: to-image f] 
                           
                ]       
            ]
        ]
    ]
    text 150 {classical fire demo for REBOL^/
 press on fire to see other effects.^/   
 Written by ReBolek, 2001 in 15 mins.^/
 We need new category on Assembly:^/
 less-than-kb-demo ;-)} with [font: make font  [size: 9]]
===Bezier 

Oldes Bezier Line Demo

See script library for %bezier-curve.r

Uses functions and data initialized at script startup

The end points are draggable to change the curve!!!!


Here a do block is used to allow executable lines for initialization 
purposes.

 do [

    draw-beziere-curve: has [result pp x0 x1 x2 x3 y0 y1 y2 y3 cx bx 
    ax cy by ay t tx ty s] [
    result: make block! 120
    pp: p0/size/x / 2
    x0: p0/offset/x + pp
    y0: p0/offset/y + pp
    x1: p1/offset/x + pp
    y1: p1/offset/y + pp
    x2: p2/offset/x + pp
    y2: p2/offset/y + pp 
    x3: p3/offset/x + pp
    y3: p3/offset/y + pp

    insert result compose [
        pen 155.0.0
        line (p0/offset + pp) (p1/offset + pp)
        line (p2/offset + pp) (p3/offset + pp)
        pen 255.255.255 line (p0/offset + pp)
    ]    
    cx: 3 * (x1 - x0)
    bx: 3 * (x2 - x1) - cx
    ax: x3 - x0 - cx - bx
    cy: 3 * (y1 - y0)
    by: 3 * (y2 - y1) - cy
    ay: y3 - y0 - cy - by
    
    t: s: 0.01 ;this value sets quality of the curve
    
    while [t <= 1][
        tx: to integer! (

                (ax * (t * t * t)) + (bx * (t * t)) + (cx * t) + .5
            ) + x0
        ty: to integer! (

                (ay * (t * t * t)) + (by * (t * t)) + (cy * t) + .5
            ) + y0

        t: t + s
        insert tail result to pair! reduce [tx ty]
    ]
    return result
 ]


 click?: false
 mouse-pos: 0x0


 ]

    origin 0

    bkg: box black 400x400 with [effect: reduce ['draw make block! 120]]
    style point box 10x10 with [

        effect: [draw [pen 0.255.0 fill-pen 0.200.0 circle 4x4 4]]
        changes: [offset]
        feel: make feel [
            engage: func [f a e][
                if a = 'down [click?: on mouse-pos: e/offset]
                if a = 'up   [click?: off]
                if find [over away] a [
                    if click? [
                        f/offset: f/offset + e/offset - mouse-pos
                        bkg/effect/2: draw-beziere-curve
                        show [bkg f]
                    ]
                ]
            ]
        ]
    ]
    at 300x200 p0: point
    at 200x100 p1: point
    at 200x300 p2: point
    at 100x200 p3: point
    do [bkg/effect/2: draw-beziere-curve]
                   
===Buttons Galore

Buttons galore from the library script %buttons.r


Here a do block is used to execute the initialization needed within 
the layout block.



    do [
        group: ["rotary" "test" "button"]
    ]

    origin 20x10
    backdrop effect [gradient 0x1 100.20.0]

    vh1 "52 Button Click-up - Each with a different click effect..."

    vtext bold "Here is a small sampling of the thousands of button effects 
    you can create. (This is 78 lines of code.)"
    at 20x80 guide
    button "simple"
    button form now/date
    button "colored" 100.0.0
    button "text colored" font [colors: [255.80.80 80.200.80]]
    button with [texts: ["up text" "down text"]]
    button "bi-colored" colors [0.150.100 150.20.20]

    button with [texts: ["up color" "down color"] colors: [0.150.100 
    150.20.20]]
    button "image" pic
    button "color image" pic 200.100.50

    button "flip color" pic with [effects: [[fit colorize 50.50.200][fit 
    colorize 200.50.50]]]
    button "blink" with [rate: 2 colors: [160.40.40 40.160.40]]
    return

    button "multiply" pic with [effects: [[fit][fit multiply 128.80.60]]]
    button "brighten" pic with [effects: [[fit][fit luma 80]]]

    button "contrast" pic with [effects: [[fit][fit contrast 80]]]
    button "horiz flip" pic with [effects: [[fit][fit flip 1x0]]]

    button "vert reflect" pic with [effects: [[fit][fit reflect 0x1]]]
    button "invert" pic with [effects: [[fit][fit invert]]]

    button "vert grad" with [effects: [[gradient 0x1 0.0.0 0.200.0] [gradient 
    0x1 0.200.0 0.0.0]]]

    button "horiz grad" with [effects: [[gradient 1x0 200.0.0 200.200.200][gradient 
    1x0 200.200.200 200.0.0]]]

    button "both grad" with [effects: [[gradient 1x0 140.0.0 40.40.200] 
    [gradient 0x1 40.40.200 140.0.0]]]

    button "blink grad" with [rate: 4 effects: [[gradient 1x0 0.0.0 0.0.200] 
    [gradient 1x0 0.0.200 0.0.0]]]

    button "blink flip" pic with [rate: 8 effects: [[fit][fit flip 0x1]]]
    return
    button "big dull button with several lines" 100x80 0.0.100

    button "dual color" pic 50.50.100 100.50.50 100x80 with [edge: [color: 
    80.80.80]]

    button "big edge" pic 100x80 with [edge: [size: 5x5 color: 80.80.80] 
    effects: [[fit colorize 50.100.50][fit]]]

    button "oval reflect" pic 50.100.50 100x80 with [effect: [fit reflect 
    1x0 oval]]
    return

    button "text on top" pic 100x80 with [font: [valign: 'top] effects: 
    [[fit gradcol 1x1 200.0.0 0.0.200] [fit gradcol -1x-1 200.0.0 0.0.200]]]

    button "text on bottom" pic 100x80 50.50.100 with [font: [valign: 
    'bottom] effects: [[fit][fit invert]]]

    button "big text font" pic 100x80 with [font: [size: 24] effects: 
    [[fit multiply 50.100.200][fit]]]

    button "cross flip" pic 50.100.50 100x80 with [effect: [fit flip 
    0x1 reflect 0x1 cross]]
    return
    toggle "toggle"
    toggle "toggle red" 100.0.0 
    toggle "toggle up" "toggle down"
    toggle "toggle colored" 0.150.100 150.20.20
    toggle "up color" "down color" 0.150.100 150.20.20

    toggle "toggle multiply" pic with [effects: [[fit][fit multiply 128.80.60]]]

    toggle "toggle contrast" pic with [effects: [[fit][fit contrast 80]]]
    toggle "toggle cross" pic with [effects: [[fit][fit cross]]]

    toggle "toggle v-grad" with [effects: [[gradient 0x1 0.0.0 0.200.0] 
    [gradient 0x1 0.200.0 0.0.0]]]

    toggle "toggle h-grad" with [effects: [[gradient 1x0 200.0.0 200.200.200][gradient 
    1x0 200.200.200 200.0.0]]]

    toggle "toggle both" with [effects: [[gradient 1x0 140.0.0 40.40.200] 
    [gradient 0x1 40.40.200 140.0.0]]]
    return
    rotary data group
    rotary data reduce [now/date now/time]
    rotary data group 100.0.0 0.100.0 0.0.100

    rotary data group with [font: [colors: [255.80.80 80.200.80]]]
    rotary data group with [colors: [0.150.100 150.20.20]]
    rotary data group pic
    rotary data group pic 200.100.50

    rotary data group pic with [effects: [[fit colorize 50.50.200][fit 
    colorize 200.50.50]]]

    rotary data group with [effects: [[gradient 0x1 0.0.0 0.200.0] [gradient 
    0x1 0.200.0 0.0.0]]]

    rotary data group with [effects: [[gradient 1x0 200.0.0 200.200.200][gradient 
    1x0 200.200.200 200.0.0]]]

    rotary data group with [effects: [[gradient 1x0 140.0.0 40.40.200] 
    [gradient 0x1 40.40.200 140.0.0]]]
===Paint Program


This section is a clip of the layout portion of Frank Sievertsen's 
remarkable paint program. Open this example to enable a quick link 
to the real source:


 button "Browse Source" [browse http://www.reboltech.com/library/html/paint.html]
 button "Close" [unview]


In the example below, a DO block is used to execute initialize code.

 do [

    color: fill-color: start: draw-image: draw-pos: tmp: none
    type: 'box
    undos: [] redos: []
    draw: func [offset /local tmp] [
        compose [
            pen (color/color) fill-pen (fill-color/color)
            (type) (start) (either type = 'circle [
                tmp: offset - start
                to-integer square-root add tmp/x ** 2 tmp/y ** 2
            ] [offset])
        ]
    ]
 ]
 
        backdrop effect compose [gradient 1x1 (sky) (water)]
        across
        draw-image: image white 300x300 effect [draw []]
        feel [engage: func [face action event] [
            if all [type start] [
                if find [over away] action [
                    append clear draw-pos draw event/offset
                    show face
                ]
                if action = 'up [
                    append/only undos draw-pos
                    draw-pos: tail draw-pos
                    start: none
                ]
            ]
            if all [type action = 'down] [
                start: event/offset
            ]
        ]]
        do [draw-pos: draw-image/effect/draw]
        guide
        style text text [
            tmp: first back find face/parent-face/pane face
            tmp/feel/engage tmp 'down none
            tmp/feel/engage tmp 'up none
        ]
        label "Tool:" return
        radio [type: 'line] text "Line"
        return
        radio [type: 'box] on text "Box"
        return
        radio [type: 'circle] text "Circle"
        return
        style color-box box 15x15 [

            face/color: either face/color [request-color/color face/color] [request-color]
        ] ibevel
        color: color-box 0.0.0 text "Pen"
        return
        fill-color: color-box text "Fill-pen"
        return
        button "Undo" [if not empty? undos [
            append/only redos copy last undos
            draw-pos: clear last undos
            remove back tail undos
            show draw-image
        ]]
        return
        button "Redo" [if not empty? redos [
            append/only undos draw-pos
            draw-pos: insert draw-pos last redos
            remove back tail redos
            show draw-image
        ]]
===Font Lab

Carl's Font lab



Here a do block is used to initialize some values needed in the layout

 do [

    change-styles: func [style start facet subfacet value /local v][
    start: find style/pane start
    foreach f start [
        f: in f facet
        if subfacet <> 'none [f: in get f subfacet]
        either block? value [

            if not block? get f [set f either none? get f [copy []][reduce [get 
            f]]]

            either v: find get f value [remove v][head insert get f value]
        ][set f value]
    ]
    show style
 ]

 chg: func ['facet 'subfacet value] [
    change-styles external-view norm-start facet subfacet value
 ]
 shad: does [chg font shadow sdir * to-integer sl2/data * 16]
 sdir: 1x1
 sz: 180x40
 sx2: sz/x / 2 
 ]



    style tgl toggle 60
    style lab vtext bold
    backcolor rebolor
    space 0x5
    across 

    p: choice 180 "Sans-Serif Style" "Serif Style" "Fixed Width Style" 

        [chg font name pick reduce [font-sans-serif font-serif font-fixed] 
        index? p/data]
        return
    tgl "Bold" [chg font style [bold]]
    tgl "Italic" italic [chg font style [italic]]
    tgl "Lined" underline [chg font style [underline]]
    return
    tgl "Left" of 'tg1 [chg font align 'left]
    tgl "Center" of 'tg1 [chg font align 'center]
    tgl "Right" of 'tg1 [chg font align 'right]
    return
    tgl "Top" of 'tg2 [chg font valign 'top]
    tgl "Middle" of 'tg2 [chg font valign 'middle]
    tgl "Bottom" of 'tg2 [chg font valign 'bottom]
    return
    lab "Size:" 60x20 font []

    sl: slider 120x20 [chg font size max 8 to-integer sl/data * 40] 
     with [append init [data: .5]]
    
    return
    lab "Space:" 60x20 font []

    sl1: slider 120x20 [chg font space (1x0 * to-integer sl1/data * 20) 
    - 5x0]
    return
    lab "Shadow:" 60x20 font []
    sl2: slider 120x20 [shad]  with [append init [data: .5]]
    return
    lab "Shad Dir:" 60x20
    arrow left  [sdir: sdir * 0x1 + -1x0 shad] pad 6
    arrow right [sdir: sdir * 0x1 + 1x0 shad]  pad 6
    arrow up    [sdir: sdir * 1x0 + 0x-1 shad] pad 6
    arrow down  [sdir: sdir * 1x0 + 0x1 shad]  pad 6
    return
    button sx2 "Text Color" [chg font color request-color]
    button sx2 "Area Color" [chg color none request-color]
    return

    button sx2 "Help" [alert "Click the controls on the left to change 
    text on the right."]
    button sx2 "Close" #"^Q" [unview]
    below
    at p/offset + (p/size * 1x0) + 10x0
    norm-start:
    Title "Title" sz
    h1 "Heading 1" sz
    h2 "Heading 2" sz
    h3 "Heading 3" sz
    h4 "Heading 4" sz
    h5 "Heading 5" sz
    at norm-start/offset + (norm-start/size * 1x0) + 10x0
    banner "Banner" sz
    vh1 "Video Heading 1" sz
    vh2 "Video Heading 2" sz
    vh3 "Video Heading 3" sz
    vtext "Video Text" sz
    text "Document Text" sz
    

===Windows Clipboard


---Cut or Copy to Clipboard


Normal Windows cut and copy commands are supported e.g. on a field, 
contents can be copied to the clipboard. Programmatic access is also 
supported for text contents.

    across 
    label "Entry field: "
    return input-field: field 200 "Enter your text here"

    return button 200 "Copy Entry field data to clipboard" [write clipboard:// 
    input-field/text]

    return button 200 "Show Clipboard Contents" [alert read clipboard://] 


---Clearing The Clipboard


    across 
    button 200 "Clear The Clipboard" [write clipboard:// ""]

    return button 200 "Show Clipboard Contents" [alert read clipboard://] 



---Paste from Clipboard


Normal Windows paste commands are supported e.g. on a field, contents 
can be pasted. Programmatic access is also supported for text contents.

    across 

    button 200 "Show Clipboard Contents" [alert read clipboard://] 
===Requesters


REBOL View supports an assortment of requesters. 


The results of the request-* code are returned as its value e.g. 
chosen-date: request-date


---Request Yes | No | Cancel


Provides the user the capability to pick from choices "Yes" | "No" 
| "Cancel"

The result is "True" | "False" | none

    do [user-response: none]

    button "Simple Request" 200 [user-response: request "Do you want 
    to abandon your input so far?"]
    button "View User Response" 200 [alert form user-response]
    

---Pick A Color

    do [chosen-color: gold] 
    button "Pick Color" 200 [chosen-color: request-color]
    button "View Chosen Color" 200 [alert form chosen-color]



---Pick An Answer

The request allows a descriptive value then 1, 2, or 3 options.


    button "Format" 100 [request ["Your message goes here. It will wrap 
    if it is very very long." "Choice 1" "Choice 2" "Choice 3"]]
    

    button "Example 1" 100 [request ["Pick The Color of Your New Model 
    T" "Black"]]
    

    button "Example 2" 100 [request ["Pick one country" "England" "France"]]


    button "Example 3" 100 [request ["Run Extract Script?" "Yes" "No" 
    "Cancel"]]


---Pick A Date

    do [chosen-date: 01-Jun-1990]
    button "Pick Date" 200 [chosen-date: request-date]
    button "See Chosen Date" 200 [alert form chosen-date]

---Get A LogonID and Password

    do [credentials: none]
    button "Get Credentials" 200 [credentials: request-pass]
    button "View Credentials" 200 [
        view/new layout [
            size 200x200 backtile polished orange 
            across banner "Credentials" 
            return label "LogonID:  " txt pick credentials 1
            return label "Password: " txt pick credentials 2
        ]
    ]


---Pick A File


Format: REQUEST-FILE /title title-line button-text /file name /filter 
filt /keep    

        do [filter-block: ["*.gif" "*.jpg" "*.png" "*.bmp"]]

        button "Pick Any File" 300 [request-file "Select"]      

        button "Pick With A Title" 300 [request-file/title "Pick The Data 
        File to Process" "OK"]

        button "Change the Action Button Name" 300 [request-file/title "Pick 
        The Data File to Process" "OK"]        

        button "Keep Results" 300 [request-file/title/keep "Previous Select 
        On This Button Is Kept" "OK"]

        button "Filter Files" 300 [request-file/title/filter "Pick An Image 
        File" "OK" filter-block]    



---Request Text Input

Format: REQUEST-TEXT /offset xy /title title-text /default str
    

    button "Request Text Input - all default parameters" 300 [request-text]

    button "Request Text Input - with offset to window" 300 [request-text/offset 
    40x40]

    button "Request Text Input - with title" 300 [request-text/title 
    "Input your question"]

    button "Request Text Input - with default" 300 [request-text/default 
    "Key your question here"]

    button "Request Text Input - with all parameters" 300 [request-text/offset/title/default 
    100x100 "Input your question" "Key your question here"]



---Request Download from Net


Request a file download from the net. Show progress. Return none 
on error.

Format: REQUEST-DOWNLOAD url /to local-file

    backtile polished orange    

    button "Request File Download To local REBOL Cache" 300 [request-download 
    http://www.rebol.com/index.html]

    button "Request File Download To This Directory" 300 [request-download/to 
    http://www.rebol.com/index.htmlnone]

    button "Request File Download To Specific File" 300 [request-download/to 
    http://www.rebol.com/index.html%/c/temp.html]

===Message Box


    button "Format" 100 [request ["Your message goes here. It will wrap 
    if it is very very long and tedious." "Close"]] 
    button "Example" 100 [request ["You done good!" "OK"]]



---Confirmation

    button "Exit" 100 [
        request/confirm "Do you want to quit without saving?" []
    ]
    

===Calling the Editor

The REBOL editor is now callable with the editor function

    backtile polished
    button 300 "Create a test file and edit it" [
        write %temp.txt "This is a test file"
        editor %temp.txt
    ] frame 204.0.0 
    

===Calling Windows

With View/Pro the calling of executables is supported.

Here are two simple examples that will work if you have View/Pro 
on a platform where a notepad and calc are avaiable.

    across backtile size 200x200
    return button "Notepad" [call ["notepad.exe"]]
    return button "Calculator" [call ["calc.exe"]]


===Window Options


Note that these are options which are ignored by the easyvid.r code 
that displays them in this tutorial.
Copy the code out and run it standalone in REBOL/View.

---Block Options: No Border and No Title

    view/options layout [
        size 200x200 
        banner "Window Options" 
        button "Close" [unview]
        ] [
            no-border
            no-title
        ]


---Word Option: No Title


Note that the results of this are surprising if you run it from within 
a script that has a title option. It is displayed near location 0x0 
of the resulting window instead of in the window frame that has been 
suppressed. 

    view/options layout [
        size 200x200 
        banner "Window Options" 
        button "Close" [unview]
        ] 'no-title

===REBOL/View Notifiers


REBOL/View supports simple notifiers to send messages to a user interface


---Alert

    button 220 polished "Send alert message" [
        alert "This causes a dialogue box to popup"
    ]





---Flash

Flash is provided to provide a message and keep on processing.

    across size 200x200
    return button 150 "Create Flash Message" [flash "Testing"]
    return button 150 "Unview Flash" [unview]



---Inform

    inform layout  [
        backtile polished sky 

        across text font-size 16 bold underline red "Action complete!" 
        return button "OK"  [unview]]

---Popup

REBOL supports popups  (see note below before running!)

        across size 200x200 
        button "Show Popup" [
            show-popup popup-layout: layout [
                    across size 200x200 
                    backtile polished
                    banner "The Popup Worked" 
                    return button "Unview" [unview]
                ]
        ]
        return button "Hide Popup" [unview/only popup-layout]

I have had some difficulties (process lockup) when using

these popups so just use view layout [...] and skip the popup part.

===Diagram Example


Carl has created some diagrams in REBOL using styles to make an architecture 
diagram.

This is a slightly modified version.


Here again a DO block precedes the layout code for non-layout initiatiation 
... here the definition of a function.

Why make a diagram this way?


1. One reason is that it can be interactive ... the sections are 
all "hot" with a few lines of code.  Here they pop up REBOL Dialogs 
but they could do anything that can be coded even something as simple 
as launching a browser on a different URL for each diagram component. 
 The "Compositor" box demonstrates this by launching your browser 
on the REBOL.com site.


2. Very small footprint size compared to other presentation source 
formats.




 do [
        information: func [info [string!]][
        request/ok reform [ info]
    ]
 ]



    style bx box 255.255.255 0.0.0 font-size 11 font [color: 0.0.0 shadow: 
    0x0] edge [size: 5x2] 
        [request/ok reform ["No information on" face/text]] 

    style bb box bold left top para [origin: 6x10] edge [size: 2x2]
        [request/ok reform ["No information on" face/text]]
    backcolor silver + 30
    at 15x15 h1 486 left "Arch Structure" 
    at 15x50    bb "Client" 506x436 160.80.80 [

        information "Any client machine e.g. branch or Call Centre"]

    at 25x252   bb "Mid-Tier" 486x68 effect [gradient 1x1 169.91.155 
    80.45.75]

    at 25x152   bb "UI" 486x96    effect [gradient 1x1 38.156.82 19.78.41]

    at 25x324   bb "Servers" 486x151   effect [gradient 1x1 103.96.200 
    50.45.100] [

        information "Mid-tiers servers with XYZ relational database server" 
                                                                         
              
    ]

    at 130x216  bx "Compositor" 182x24 bold [browse http://www.rebol.com]

    at 130x60   bx "Browser" 120x24 [information "Branch standard browser"]

    at 130x188  bx "Sound" 182x24 bold [information "Sound services"]
    at 255x60   bx "Win32" 120x24 [information "Win32 App"]

===Column Images


Creates a layout looking (a little) like columns. It uses a gradient 
effect going from darker to lighter

 do [
     column: make image! layout [

            backdrop effect [gradient 1x0 20.20.20 250.240.230 luma 60]
        ]

    column-size: 50x420

    area-size: 400x420  ; height should be the same as column-size
 ]
 backtile polished tan
   across 
   image column-size  column 
   pad -10x0        ; this brings the default VID spacing back
   area wrap area-size  

   edge none        ; take the edge off of area so that it more closely 
   blends 
   shadow 2x2

   pad -10x0 image column-size column  ; if you want a right column

===Tree View of Directory

This is Didier's tree view %request-dir.r


In this sample, you must be online because the code is accessed on 
the Rebol script server

 do [do http://www.rebol.org/library/scripts/request-dir.r
     request-dir
 ]


Note that:

* the script is read from the script library but runs locally

* it is showing the files in your directories


===The emailer Function


The function for emailing has appeared in Jan-2004 on the rebol list.


It is a simple idea ... to create a standard emailer by invoking 
a function emailer. This window will show the source:

  text wrap 400x300 mold get 'emailer

And it is simple to run:

    across size 200x200
    return button 150 "Run emailer" [emailer]


But on my machine there is again a problem - the emailer locks up 
REBOL/View.

Recommendation:

* if it works use it if you like


* use Doc Kimbel's one liner (works for me). Assumes you have set 
up your email in set-user



 e: field "Email" s: field "Subject" m: area "Body" btn "Send"[send/subject 
 to-email e/text m/text s/text alert "ok"]



* better yet, make your own... if the code for the basic is 1 line, 
then a custom version is not far away. Here's an example that allows 
selection of your frequent contacts (entered in the names-addresses 
series) and keeps a journal of email that you have sent (using this 
code) in file email-journal.txt.  Assumes you have setup your user 
profile correctly to allow sending of email.


 do [

  names-addresses: [
    "Contact 1"         [contact1-:-no-such-address-:-com]
    "Contact 2"         [contact2-:-no-such-address-:-com]
    "Contact 3"         [contact3-:-no-such-address-:-com]
  ]

  names: copy []
  foreach [name address] names-addresses [append names name]


  journal?: false  ; set to true if want to journalize sent email
 ]

    e: rotary 200 data sort names
    s: field "Subject" 
    m: area 500x400 wrap "Body" 
    btn "Send"[

        send/subject who-to: select names-addresses e/text m/text s/text 
        alert join "Sent email to: " form who-to
        
        if journal? [
            write/append %email-journal.txt rejoin [
                "[ When-sent: " now/precise 
                " To: " who-to
                " Subject: {" s/text
                "} Message: {" m/text "} ] "
                newline
            ]
        ]
    ]
    btn "Quit" [unview]



It won't take much to change this from the rotary used to a text 
list allowing multiple selections.





===Some More email


Earlier there have been a few examples of sending email. Here are 
a few more that often appear in the mailing list

---Simple Send


This is not a runnable version because you don't need anything but 
REBOL/Core to run it. It has been wrapped in a DO block so it does 
not send errors to the console.

---Quick Send Short Message

 do [
    send [address-:-isp-:-com] "My Message"
 ]
 
---Send Longer Message  

Now a more complex message where there is a body to the message:

 do [
    send [address-:-isp-:-com] {Sample Message
               
    This is the body of the message
    } 
 ]

---Send with One Attachment


Here, so that the sample does not fail, test file(s) are created 
by the code before attempting the send. 

 do [
    test-file: %file-attachment.txt
    write test-file {Just some test data to create a file}
    send/attach [address-:-isp-:-com] {Sample Message
               
    This is the body of the message
    } test-file
 ]
 
---Send with Attachments

And a message with multiple attachments.


Here, so that the sample does not fail, test file(s) are created 
by the code before attempting the send. 

 do [
    files: [%file-attachment.txt %second-attachment.txt]

    foreach file files [write file {Just some test data to create a file}]
    send/attach [address-:-isp-:-com] {Sample Message
               
    This is the body of the message
    } files
 ]
 
---Send to Multiple Addresses


Here, so that the sample does not fail, test file(s) are created 
by the code before attempting the send. 

 do [
    files: [%file-attachment.txt %second-attachment.txt]

    foreach file files [write file {Just some test data to create a file}] 

    send/attach [[address-:-isp-:-com][asecondAddress-:-isp-:-com]] {Sample Message
               
    This is the body of the message
    } files
 ]
 
---Send/only

Same send only just provide the SMTP server with one copy:

Here, so that the sample does not fail, test file(s) are created 
by the code before attempting the send. 

  do [
    files: [%file-attachment.txt %second-attachment.txt]

    foreach file files [write file {Just some test data to create a file}] 

    send/only/attach [[address-:-isp-:-com][asecondAddress-:-isp-:-com]] {Sample 
    Message
               
    This is the body of the message
    } files
 ]
 
---Send With Header


This example uses a Do block to wrap the code. If you execute the 
email should be sent.
But it is unlikely to be delivered.


The addresses for me and you should be changed in your use as well 
as the

* Subject

* Organization

* Content 

 do [
   me: [myaddress-:-isp-:-com]
   you: [youraddress-:-isp-:-com]
   header-object: make system/standard/email [
            From: me
            Reply-To: me
            Subject: "Some Stuff"
            Organization: "Cyberia"
            MIME-Version: 1.0 
            Content-Type: "text/plain"
    ]
 send/header you {Test Message
    This is the message body.
    }                 
    header-object 
 ] 

---Send with CC

This adds a copy value in the header-object

 do [
   me: [myaddress-:-isp-:-com]
   you: [youraddress-:-isp-:-com]
   header-object: make system/standard/email [
            From: me
            Reply-To: me
            Subject: "Some Stuff"
            Organization: "Cyberia"
            MIME-Version: 1.0 
            Content-Type: "text/plain"
        cc: [another-address-:-isp-:-com]
    ]
 send/header you {Test Message
    This is the message body.
    }                 
    header-object 
 ] 


   
---Doctored Code

Again Doc Kimbel's one liner that does not waste a character


 e: field "Email" s: field "Subject" m: area "Body" btn "Send"[send/subject 
 to-email e/text m/text s/text alert "ok"]


===Sharp Styles


I really like the style that Didier has put around his email previewer

 do [
    ss-light: stylize [
        text: text feel none
        vtext: vtext feel none
        col-hdg: text black 255.255.204 bold middle effect []
        col-txt: text edge [size: 1x0 color: gray effect: 'bevel]
        ban: vh3 left to-pair reduce [
            50 logo.gif/size/y] edge [

                color: 0.0.0 size: 0x1] feel none with [color: black]
        lab: label para [origin: 2x3 margin: 0x2]
        labe: lab edge [size: 1x1 color: water effect: 'ibevel]
        inf: info 100 font-color yellow
        bkg: backdrop water - 10.10.10
        txt-big: vtext 300 font-size 18 font-color yellow center
        rti: vtext font-size 14 bold
        txt-ch: rti font-color white 170x22 para [
            origin: 2x3] with [font: make font [
                    color: white] colors: [55.95.155 235.170.55]]
        btnb: btn 70.70.70 font-color white

        men: rti 264 edge [size: 1x1 color: water effect: 'bevel] para [origin: 
        20x2 margin: 1x4]

             with [color: water - 40.40.40 effect: first effects: [

                [draw [pen white fill-pen white polygon 5x2 13x10 5x18]] [draw [pen 
                white fill-pen white polygon 2x5 10x13 18x5]]
             ] feel: none]  ;system/view/vid/vid-feel/hot]

        cbox: box 60x20 edge [size: 1x1 color: water effect: 'bevel] [

            if temp: request-color/color first face/data [face/color: temp change 
            face/data temp show face]
        ] with [append init [color: first data]]
    ]
    stylesheet: ss-heavy: stylize/styles [

        col-hdg: col-hdg effect [gradcol 0x1 200.200.160 155.155.104]

        ban: ban effect [merge gradcol 150.180.200 0.0.0] with [color: none]

        bkg: backdrop effect [gradient 1x1 65.125.175 45.75.115 grid 2000x4 
        1999x4 70.130.190 blur]
        txt-big: vtext 300 font-size 18 font-color yellow center
        rti: vtext font-size 14 bold

        txt-ch: txt-ch effect [gradcol -1x1 105.105.105 151.151.151]
        men: men effect [gradcol -1x0 black water]
    ] ss-light
 ]
    styles stylesheet
    space 4x4 origin 4x4 across
    bkg
    pad 15 ban 235 :title para [origin: 32x0]
    pad -254
    image 30x30 %palms.jpg effect [fit key 255.0.255]
}
code: text: layo: external-view: none
sections: []
layouts: []
space: charset " ^-"
chars: complement charset " ^-^/"

rules: [title some parts]

title: [text-line (title-line: text)]

parts: [
      newline
    | "===" section
    | "---" subsect
    | "!" note
    | example
    | paragraph
]

text-line: [copy text to newline newline]
indented:  [some space thru newline]
paragraph: [copy para some [chars thru newline] (emit txt para)]
note: [copy para some [chars thru newline] (emit-note para)]
example: [
    copy code some [indented | some newline indented]
    (emit-code code)
]

section: [
    text-line (
        append sections text
        append/only layouts layo: copy page-template
        emit h1 text
    ) newline
]
subsect: [text-line (emit h2 text)]
emit: func ['style data] [repend layo [style data]]
emit-code: func [code] [
    remove back tail code
    repend layo ['code 460x-1 trim/auto code 'show-example]
]
emit-note: func [code] [
    remove back tail code
    repend layo ['tnt 460x-1 code]
]

show-example: [

    if external-view [xy: external-view/offset  unview/only external-view]
    xcode: load/all face/text
    if not block? xcode [xcode: reduce [xcode]] ;!!! fix load/all
    if here: select xcode 'layout [xcode: here]
    external-view: view/new/offset layout xcode xy
]

page-template: [
    size 500x480 origin 8x8
    backdrop white - 80

    style code tt snow navy bold as-is para [origin: margin: 12x8]
    style tnt txt maroon bold
]

parse/all detab content rules
show-page: func [i /local blk last-face][
    i: max 1 min length? sections i
    append clear tl/picked pick sections i 
    if blk: pick layouts this-page: i [
        f-box/pane: layout/offset blk 0x0 
        last-face: last f-box/pane/pane    ; bh slider

    f-box/pane/pane/1/size: f-box/pane/size: max 500x480 add 20x20 add 
    last-face/offset last-face/size ; bh slider
    update-slider ; bh slider
        show f-box
    ]

    show tl    ; changed to after slider update ; was not refreshing 
    the index display
]


update-slider: does [
    sld/data: 0
    either object? f-box/pane [
        sld/redrag min 1.0 divide sld/size/2 f-box/pane/size/2
        sld/action: func[face event] compose [

            f-box/pane/offset/2: multiply face/data (subtract 480 f-box/pane/size/2)
            show f-box
        ]
    ][
        sld/redrag 1.0 show sld
        sld/action: none
    ]
    show sld
]

main: layout [
    backtile polished
    across
    vh2 title-line return
    tl: text-list 160x480 bold black white data sections [
        show-page index? find sections value
    ]
    h: at
    f-box: box 500x480

  at h + 500x0 sld: slider 24x480                 ; add brett's slider
    at h + 456x-24
    across space 4
    arrow left  keycode [up left] [show-page this-page - 1]
    arrow right keycode [down right] [show-page this-page + 1]
    pad -150

    txt white italic font-size 16 form system/script/header/date/date
]

show-page 1
xy: main/offset + either system/view/screen-face/size/x > 900 [
    main/size * 1x0 + 8x0][300x300]
view main
BrianH:
11-Apr-2008
Keep in mind that the words local to a function's context are only 
really valid during the execution of the function. When referred 
to from outside the function, their bindings are valid but their 
assigned values are not, and will be overridden on next run.
Gregg:
18-Oct-2008
From !REBOL3 group, following MattAnton's fbionacci func.


Matt, it's a good func, but there are some things to watch out for 
in REBOL, which are different from many other languages.


1) Undeclared vars in func become global. Use the /local refinement 
to declare them.


2) Series values in funcs (e.g. your starting block of [0.0 1.0] 
maintain their value between calls if you don't use COPY. Run your 
function multiple times to see what happens. It may be that you wanted 
this to be a memoizing function, but then why UNSET 'fibonacci-block?.


I think you also mentioned that the challenge was to do it recursively, 
which this isn't. That's a case where you would definitely want to 
memoize. :-)


In any case, this is always fun stuff to think about.  Here's a modified 
version for you to play with. Look at some of the other REBOL funcs 
used, see if you find any bugs, or maybe it will give you ideas for 
other ways to solve the problem.

fibonacci: func [

    "Returns a list of fibonacci numbers, up to the specified count."
	count [integer!] "Number of iterations to run"
	/trace
	/local res n-1 n-2 incr step
] [
    incr: func [word] [set word 1 + get word]
    step: does [incr 'n-1  incr 'n-2]
	res: copy [0.0 1.0]
	set [n-1 n-2] [1 2]
	repeat i count [
		append res add pick res n-1 pick res n-2
		step
		if trace [print [i last res]]
    ]
    res
]
print mold fibonacci 3
print mold fibonacci 46
Dockimbel:
18-Oct-2008
Here's my attempt with a caching and fully recursive version :

fibonacci: func [n /local f][
	f: [0.0 1.0]
	either f/(n + 1) [copy/part f n + 1][
		fibonacci n - 1
		append f f/:n + f/(n - 1)
	]
]

probe fibonacci 3	
probe fibonacci 46
probe fibonacci 8

Note that :


1) The last call with 8 value is just an extraction of the pre-computed 
cached sequence (cached values up to 46th by the previous call), 
so it executes in 0(1).


2) References to 'n (except fibonacci n - 1) are incremented by 1 
to account for REBOL series 1-based indexes. If we could switch to 
0-based indexes, the function source would be more readable (closer 
to the pure algorithm).
Janko:
12-Feb-2009
para-do: func [ codes- /local codes cnt values code- value- all-done 
] [
  codes: copy codes-
	cnt: length? codes
  values: make block! cnt
	loop cnt [ append values none ]
	until [ 
		all-done: true
		repeat idx cnt [
			code: pick codes idx
			if not empty? code [
				set/any [ value- code- ] do/next code
				codes/:idx: code-
				values/:idx: value-
				all-done: false
				print mold value-
			]
		] all-done
	] 
	values
]
BrianH:
1-Apr-2009
No, R2 doesn't use registers. The context assigned to the function 
is reused, with the value block switched out on recursion. The original 
(non-recursive) value block is left alone when the function returns, 
and set to nones on function entry. This is where the memory leaks 
come from: unintentionally persistent references to local variables. 
R2 actually does have a stack (it's a block), but it is used differently.


R3 just has a second context type that does stack-indirect word dereferencing. 
When the function starts a block of values is pushed on the task-local 
stack and the references to those values do an additional indirection 
to get the stack frame before getting the values - this is why function! 
word dereferencing is 28% slower than object! or closure! word dereferencing.


R2 has two context types: object! and system/words (an expandable 
object!). R3 also has two context types: it doesn't have R2-style 
object! contexts - all are expandable like system/words - but it 
does add the stack-indirect type.
[unknown: 5]:
27-Apr-2009
skip+: make function! [
    {Returns a series matching the skip sequence}
    series [series!] "Series to return skip values from."
    interval [integer!] "Skip interval"
    start [integer!] "Series index to start skipping from."
    /local blk
    ][
    blk: copy []
    if interval > (length? series) [return none]
    series: at series start
    while [not tail? series][

        if (index? series) = start [insert tail blk first series start: start 
        + interval]
        series: next series
    ]
    series: head series
    if empty? blk [return none]
    blk
]
Maxim:
27-May-2009
possibly, yes, by indirection.  push stack [func args-block]

and args-block is a mem copy of the args block with local values
BrianH:
18-Dec-2009
IT could be a function that returns the thread-local top of the stack 
of implied subject values. IF would then push a value on that stack, 
and pop the value off when it returns. Might be tricky to make error-throw-safe, 
but easy to make thread-safe :)
Dockimbel:
8-Jan-2010
Janko, a function is a context! value like objects. You can use the 
following mental analogy to see how it is related :

foo: func ["for demo" a [integer!] /local b][...]


would be *roughly* equivalent to constructing an object like that 
:

foo-def: make object! [
	hidden-ctx: make object! [a: none local: none b: none]
	body: [...]
	spec: ["for demo" a [integer!] /local b]
]


The body is bound to the 'hidden-ctx context at function creation. 
When calling 'foo, the interpreter will set the 'hidden-ctx object 
words values according to passed arguments and refinements and then 
DO 'body.


There's no differences on how REBOL treats "arguments" and "local 
words", it's part of the illusion. The /local refinement is used 
by *convention* only, to set "local words", you could just decide 
to use any other refinement for the same job. Here's an example :

>> a: func [/local b][print b]
>> a/local 5
5


Additionnaly, when you apply the ordinal natives on a function! value, 
you get :

>> first :foo
== [a /local b]		;=> the hidden context words

>> second :foo
== [...]			;=> the function body block

>> third :foo
== ["for demo" a [integer!] /local b]	;=> the original spec block
BrianH:
21-Jan-2010
>> spec-of :remove-each
== [

    {Removes values for each block that returns true. Returns remove 
    count. (Modifies)}

    'word [word! block!] "Word or block of words to set each time (local)"
    data [series!] "The series to traverse"
    body [block!] "Block to evaluate (return TRUE to remove)"
]
Gregg:
27-Jan-2010
forskip+: func [
        "Like FORSKIP, but with local FIRST? and LAST? support."
        [throw catch]

        'word [word!] {Word set to each position in series and changed as 
        a result}
        skip-num [integer!] "Number of values to skip each time"
        body [block!] "Block to evaluate each time"
        /local orig result
    ][

        if not positive? skip-num [throw make error! join [script invalid-arg] 
        skip-num]
        if not any [
            series? get word
            port? get word
        ] [

            throw make error! {forskip/forall expected word argument to refer 
            to a series or port!}
        ]
        orig: get word
        use [first? last?] [
            first?: true
            last?:  false
            body: bind/copy body 'first?

            while [any [not tail? get word (set word orig false)]] [
                if tail? skip get word skip-num [last?: true]
                set/any 'result do body
                set word skip get word skip-num
                first?: false
                get/any 'result
            ]
        ]
    ]
Ashley:
4-Apr-2010
A non-parse solution (for the replace problem) based on the existing 
replace mezz:

replace-each: make function! [
	target [series!] "Series that is being modified"
	values [block!] "Block of search/replace strings"
	/local len pos
][
	foreach [search replace] values [
		len: length? search
		while [pos: find target search] [
			target: change/part pos replace len 
		]
	]
]
Pekr:
16-Apr-2010
Did anyone do IP arithmetics? I need to check, if some ip is in correct 
range  :-)


I have e.g. IP 10.10.10.10, and I need to check, if it belongs to 
10.10.0.0/16. I have very primitive (but probably complicated function, 
which can't however count with cases where mask is different from 
8. 16, 24, or 32:

in-ip-range?: func [ip-fw ip-sq /local is? ip-sq-tmp ip-fw-tmp][


  ;--- turn ip-string into block of separated values- removes dots 
  and slash ["10" "10" "10" "10" "24"]
  ip-sq-tmp: parse ip-sq "./"
  ip-fw-tmp: parse ip-fw "."

  mask:  last ip-sq-tmp
  ip-sq: copy/part ip-sq-tmp 4
  ip-fw: copy/part ip-fw-tmp 4
   
  switch/default mask [

    "8"  [either (copy/part ip-fw 1) = (copy/part ip-sq 1) [is?: true][is?: 
    false]]

    "16" [either (copy/part ip-fw 2) = (copy/part ip-sq 2) [is?: true][is?: 
    false]]

    "24" [either (copy/part ip-fw 3) = (copy/part ip-sq 3) [is?: true][is?: 
    false]]

    "32" [either (copy/part ip-fw 4) = (copy/part ip-sq 4) [is?: true][is?: 
    false]]
  ][
     is?: false

     print ["Mas not found: " mask ", the result will most probably contain 
     false positives ..."]
  ]

 return is?

]
DideC:
17-Sep-2010
I need some help dealing with paths.

I have a block of sublocks and values refered by words.

I want to make a function that increment a value in a subblock based 
on a process number and a path. But adding subpath to a path seems 
to work only with file! type.

I hope that the code bellow obviously show what I want :

values: [

 1 [dos [new 0 modified 0 deleted 0] fic [new 0 modified 0 deleted 
 0]]

 2 [dos [new 0 modified 0 deleted 0] fic [new 0 modified 0 deleted 
 0]]
]

inc-counter: func [process path /local p] [
	p: select values process
	p/(path): 1 + p/(path)
]

inc-counter 1 'dos/new
inc-counter 1 'dos/new
inc-counter 2 'dos/deleted
inc-counter 2 'fic/modified
Sunanda:
17-Sep-2010
The code below seems to do what you want......Just lacks the clever 
optimisation that'll turn it into a one-liner :)

inc-counter: func [process path /local p] [
    p: select values process
    path: parse mold path "/"
    p: select p to-word path/1
    p: find p to-word path/2
    p/2: p/2 + 1
]
sqlab:
17-Sep-2010
inc-counter: func [process path /local p] [
	p: select values process
	p: select p first path
	change next find p second path 1 + p/(second path)
]
Sunanda:
17-Sep-2010
Thanks Ladislav and sqlab -- I was having a blindspot about being 
able to directly access the parts of a path.


DideC -- if you need to go to any depth, this version may help (subject 
to optimisation by the gurus):

inc-counter: func [process path /local p] [
    p: select values process
    foreach pp copy/part path -1 + length? path [
        p: select p pp
        ]
    p: find p to-word last path
    p/2: p/2 + 1
]
Anton:
17-Sep-2010
If you are happy to use issues instead of integers for your process 
ids, then maybe this would be ok:

values: [

 #1 [dos [new 0 modified 0 deleted 0] fic [new 0 modified 0 deleted 
 0]]

 #2 [dos [new 0 modified 0 deleted 0] fic [new 0 modified 0 deleted 
 0]]
]

inc-counter: func [process path /local p] [

 do reduce [to-set-path p: join 'values/(to-issue process) path p 
 '+ 1] 
]

inc-counter 1 'dos/new
inc-counter 1 'dos/new
inc-counter 2 'dos/deleted
inc-counter 2 'fic/modified
Gregg:
17-Sep-2010
I was thinking along the same lines as Anton. 

values: [

 1 [dos [new 0 modified 0 deleted 0] fic [new 0 modified 0 deleted 
 0]]

 2 [dos [new 0 modified 0 deleted 0] fic [new 0 modified 0 deleted 
 0]]
]

inc-counter: func [key path /local rec] [
	rec: select values key
	path: head insert copy path 'rec
	do reduce [to set-path! path  1 + do path]
]

inc-counter 1 'dos/new
inc-counter 1 'dos/new
inc-counter 2 'dos/deleted
inc-counter 2 'fic/modified
Anton:
27-Sep-2010
sforpath: func ["Evaluate a path similar to the builtin path evaluation, 
except number elements SELECT (rather than PICK)."
	path [path!] action [function! block!] /local e v c
][

 v: get path/1 ; The path is assumed to begin with a word, so get 
 its value.

 while [not tail? path: next path][ ; Step through the path to inspect 
 each of its elements.
		c: v ; Store the current value before SELECTing into it.
		e: pick path 1 ; The current element.
		;print [mold :e mold type? :e]
		if get-word? :e [e: get e]
		case [

   number? e [v: select v e] ; SELECT this number element. (Paths normally 
   PICK number elements.)

   word? e [v: select v e] ; SELECT this word element (as paths normally 
   do).
		]
	]
	;?? e ?? v ?? c
	; Process the last element.
	if block? :action [action: func [c e] action]
	action c e
]
; Test
values: [1 [dos [new 0]]]

sforpath 'values/1/dos/new [c/:e: c/:e + 1]  ; <- DideC's INC-COUNTER 
function could be implemented simply using this.
Group: View ... discuss view related issues [web-public]
Maxim:
21-Feb-2007
I've juste noticed the nice values we get for event key when pressing 
function keys !!!

in order: console listen tcp udp icmp dns local odbc oracle
Geomol:
5-Nov-2008
Henrik, the hsv2rgb function, I made for Canvas RPaint. It takes 
H S V values as decimals in the area 0.0 - 1.0 as input:

hsv2rgb: func [
	H S V
	/local
		RGB
		var_h var_i
		var_1 var_2 var_3
		var_r var_g var_b
][
	RGB: 0.0.0
	either S = 0		;HSV values: 0 Ö 1
	[
		RGB/1: to-integer V * 255
		RGB/2: to-integer V * 255
		RGB/3: to-integer V * 255
	][
		var_h: H * 6
		if var_h >= 6 [var_h: 0]		;H must be < 1
		var_i: to-integer var_h
		var_1: V * (1 - S)
		var_2: V * (1 - (S * (var_h - var_i)))
		var_3: V * (1 - (S * (1 - (var_h - var_i))))
	
		switch var_i [
			0 [var_r: V			var_g: var_3	var_b: var_1]
			1 [var_r: var_2		var_g: V		var_b: var_1]
			2 [var_r: var_1		var_g: V		var_b: var_3]
			3 [var_r: var_1		var_g: var_2	var_b: V	]
			4 [var_r: var_3		var_g: var_1	var_b: V	]
			5 [var_r: V			var_g: var_1	var_b: var_2]
		]
	
		RGB/1: to-integer var_r * 255		;RGB results: 0 Ö 255
		RGB/2: to-integer var_g * 255
		RGB/3: to-integer var_b * 255
	]
	RGB
]
Group: I'm new ... Ask any question, and a helpful person will try to answer. [web-public]
Henrik:
21-Jun-2006
I made a function for this to interface MySQL:

keyed: func [keys [block!] values [block!] /local out] [
  out: copy []
  if not any [empty? keys empty? values] [
    repeat i length? keys [
      insert tail out reduce [

        keys/:i either block? first values [values/1/:i][values/:i]
      ]
    ]
  ]
  out
]
RobertS:
14-Sep-2007
I realized there was this traversal option using a lit-path! treated 
as a series! but it did not seem to if what I already had was a path! 
 held by a word and I wanted to 'extend' that value with a word.

This arises when the embedded word becomes bound to a different block. 
 In that case an OBJECT! looks to be the only option but then the 
WORDSs in the PATH come already bound to values and so are not 'functors' 
as are 'a 'd and 'e in your example.

I  want to construct a resultant valid path! from a valid path! + 
a lit-word where that word has no value but serves only as functor.

I had hoped that the func to-lit-path would be the answer, but I 
see now that the default Rebol DO path! evaluation precludes this 
kind of 'append'.

I should be able to use a modified version of your eval-path func 
to take as args a valid path! and a word!

My path idea is more like a 'tilde' than our '/' such that I can 
have
        ; blk/key~wrd1~wrd2~wrd3 ... ~wrd-n     ; e.g.,  
    path~wrd1~wrd-i~wrd-j ~wrd-k    ; becomes
; ...
    path2~wrd-m~wrd-n  ;  i.e.,
        ; blk/key/putative-confirmed-key~wrd-m~wrd-n   
PARSE is likely part of the answer if I go that TILDE route.
Once I have a lit-path! your eval-path is the traversal.
A blk of args to a func such as

  construct_dpath: func  [ dpath [lit-path!]  functor-words-blk  [block! 
  ]  /local v1 v2] [ 

should model my case OK and that dpath can be constructed by modified 
versions of your eval-path.  Thanks
mhinson:
30-Apr-2009
Hi, thanks for the extra puzzles.


I have managed to write a version that works now, but I am frustrated 
because I cant understand how to keep variables local & pass them 
to my function & return the changed values back...  In previous programming 
experience I seem to remeber that the function header listed the 
variable names & types used localy, then the function was called 
with the variables of the right type in the right order.. I cant 
remeber how results were returned in that context.


I have been reading here http://www.rebol.com/docs/core23/rebolcore-9.html#section-3.5
but every way I try seems to stop the code working as expected.

raw-data: [1 2 3 10 11 99 101 2000 2001 2002 2003 2004]

sequence-break: func [][
	either (count > 1) [
		append result to-pair rejoin [store "x" count]
		count: 1
		reduce [count result]
	][
		append result store
		reduce result
	]
]

compress: func [raw-data][
	count: 1
	store: reduce raw-data/1
	result: copy []

 repeat i ((length? raw-data) - 1) [either ((raw-data/(i) + 1) = (raw-data/(i 
 + 1))) [
			count: count + 1 
		][
			sequence-break count result store
			store: reduce raw-data/(i + 1)
		]
	]
	sequence-break count result store
	reduce result
]

print compress raw-data
mhinson:
30-Apr-2009
I was expecting 
funcA to be passed the values 2 & 3 
funcA refer to them as argZ & argY

FuncA to use local variables argA & argB & these variables to be 
given the content of argZ & argY
funcA calls func1 and passes it values in argZ & argY
func1 recieves values passed and refers to them as arg3 & arg4

func1 uses local vars arg1 & arg2 which are given the content of 
arg3 & arg4

func1 swaps position of values in arg1 & arg2 (so we know the function 
has worked)
arg3 & arg4 are given the values of arg1 & arg2

func1 ends and passes the changed content of argZ & argY back to 
funcA that called it, but as argA & argB
The end part of my code is wrong..  it should be 
argZ: argA argY: argB 
reduce [argZ argY]

so expecting argZ & argY to takeon the content of argA & argB

finaly passes the content of argZ & argY back to where it was called 
so the probe shows the swapped values.
mhinson:
7-May-2009
I added an extra string for the title and now get this similar failure

>> filenames: request-file/title/filter/path {Select all files to 
read} {x} [*.txt] %/D/Rebol/X/!conf/

** Script Error: Invalid argument: *.txt * *.r *.reb *.rip *.txt 
*.jpg *.gif *.bmp *.png
** Where: request-file
** Near: done: local-request-file data: reduce

[tt/text ob/text clean-path where picked filt-names filt-values found? 
any [only]...
Gregg:
11-May-2009
REBOL []

do %include.r
include %file-list.r


flash-wnd: flash "Finding test files..."

if file: request-file/only [
    files: read first split-path file
]
if none? file [halt]

items: collect/only item [
    foreach file files [item: reduce [file none]]
]

unview/only flash-wnd



;-------------------------------------------------------------------------------
;-- Generic functions

call*: func [cmd] [
    either find first :call /show [call/show cmd] [call cmd]
]

change-each: func [
    [throw]

    "Change each value in the series by applying a function to it"

    'word   [word!] "Word or block of words to set each time (will be 
    local)"
    series  [series!] "The series to traverse"

    body    [block!] "Block to evaluate. Return value to change current 
    item to."
    /local do-body
][
    do-body: func reduce [[throw] word] body
    forall series [change/only series do-body series/1]

    ; The newer FORALL doesn't return the series at the tail like the 
    old one

    ; did, but it will return the result of the block, which is CHANGE's 
    result,
    ; so we need to explicitly return the series here.
    series
]

collect: func [
    "Collects block evaluations." [throw]
    'word
    block [block!] "Block to evaluate."
    /into dest [block!] "Where to append results"
    /only "Insert series results as series"

    /local fn code marker at-marker? marker* mark replace-marker rules
][
    block: copy/deep block
    dest: any [dest make block! []]

    fn: func [val] compose [(pick [insert insert/only] not only) tail 
    dest get/any 'val

        get/any 'val
    ]
    code: 'fn
    marker: to set-word! word
    at-marker?: does [mark/1 = marker]
    replace-marker: does [change/part mark code 1]
    marker*: [mark: set-word! (if at-marker? [replace-marker])]
    parse block rules: [any [marker* | into rules | skip]]
    do block
    head :dest
]

edit-file: func [file] [
    ;print mold file

    call* join "notepad.exe " to-local-file file ;join test-file-dir 
    file
]

flatten: func [block [any-block!]][
    parse block [

        any [block: any-block! (change/part block first block 1) :block | 
        skip]
    ]
    head block
]

logic-to-words: func [block] [

    change-each val block [either logic? val [to word! form val] [:val]]
]

standardize: func [

    "Make sure a block contains standard key-value pairs, using a template 
    block"
    block    [block!] "Block to standardize"
    template [block!] "Key value template pairs"
][
    foreach [key val] template [
        if not found? find/skip block key 2 [
            repend block [key val]
        ]
    ]
]

tally: func [

    "Counts values in the series; returns a block of [value count] sub-blocks."
    series [series!]
    /local result blk
][
    result: make block! length? unique series

    foreach value unique series [repend result [value reduce [value 0]]]
    foreach value series [
        blk: first next find/skip result value 2
        blk/2: blk/2 + 1
    ]
    extract next result 2
]


;-------------------------------------------------------------------------------

counts: none

refresh: has [i] [
    reset-counts
    i: 0
    foreach item items [
        i: i + 1
        set-status reform ["Testing" mold item/1]
        item/2: random/only reduce [true false]
        show main-lst
        set-face f-prog i / length? items
        wait .25
    ]
    update-counts
    set-status mold counts
]

reset-counts: does [counts: copy [total 0 passed 0 failed 0]]

set-status: func [value] [set-face status form value]

update-counts: has [pass-fail] [
    counts/total: length? items

    pass-fail: logic-to-words flatten tally collect res [foreach item 
    items [res: item/2]]
    ;result (e.g.): [true 2012 false 232]
    standardize pass-fail [true 0 false 0]
    counts/passed: pass-fail/true
    counts/failed: pass-fail/false
]

;---------------------------------------------------------------


main-lst: sld: ; The list and slider faces
c-1:           ; A face we use for some sizing calculations
    none
ml-cnt:        ; Used to track the result list slider value.
visible-rows:  ; How many result items are visible at one time.
    0

lay: layout [
    origin 5x5
    space 1x0
    across

    style col-hdr text 100 center black mint - 20

    text 600 navy bold {

        This is a sample using file-list and updating progress as files are
        processed. 
    }
    return
    pad 0x10

    col-hdr "Result"  col-hdr 400 "File" col-hdr 100
    return
    pad -2x0

    ; The first block for a LIST specifies the sub-layout of a "row",

    ; which can be any valid layout, not just a simple "line" of data.

    ; The SUPPLY block for a list is the code that gets called to display

    ; data, in this case as the list is scrolled. Here COUNT tells us

    ; which ~visible~ row data is being requested for. We add that to 
    the

    ; offset (ML-CNT) set as the slider is moved. INDEX tells us which
    ; ~face~ in the sub-layout the data is going to.

    ; COUNT is defined in the list style itself, as a local variable 
    in
    ; the 'pane function.
    main-lst: list 607x300 [
        across space 1x0 origin 0x0
        style cell text 100x20 black mint + 25 center middle
        c-1: cell  cell 400 left   cell [edit-file item/1]
    ] supply [
        count: count + ml-cnt
        item: pick items count
        face/text: either item [
            switch index [
                1 [

                    face/color: switch item/2 reduce [none [gray] false [red] true [green]]
                    item/2
                ]
                2 [mold item/1]
                3 ["Edit"]
            ]
        ] [none]
    ]

    sld: scroller 16x298 [ ; use SLIDER for older versions of View

        if ml-cnt <> (val: to-integer value * subtract length? items visible-rows) 
        [
            ml-cnt: val
            show main-lst
        ]
    ]
    return
    pad 0x20
    f-prog: progress 600x16
    return
    status: text 500 return
    button 200 "Run" [refresh  show lay]
    pad 200
    button "Quit" #"^q" [quit]
]

visible-rows: to integer! (main-lst/size/y / c-1/size/y)

either visible-rows >= length? items [
    sld/step: 0
    sld/redrag 1
][
    sld/step: 1 / ((length? items) - visible-rows)
    sld/redrag (max 1 visible-rows) / length? items
]

view lay
Group: Parse ... Discussion of PARSE dialect [web-public]
Chris:
11-May-2008
Assuming you want to assign values to function locals from the external 
parse rules, you can a) bind as you are doing, b) create a larger 
context for the function encompassing your rules or c) compile the 
parse rule, either on creation of the function or for each instance.

a)
rule: [set tag tag!]
test: func [data /local tag][bind rule 'data parse data rule tag]

b)
test: use [tag][
    rule: [set tag tag!]
    func [data][parse data rule tag]
]

c)
rule: [set tag tag!]
test: func [data /local tag] compose/only [parse data (rule) tag]


Also, note that when you bind, it alters the original block -- no 
need to reassign to a new word.
Group: Linux ... [web-public] group for linux REBOL users
Graham:
17-Dec-2006
request-file: func [

    {Requests a file using a popup list of files and directories.} 
    /title "Change heading on request." 
    title-line "Title line of request" 
    button-text "Button text for selection" 
    /file name "Default file name or block of file names" 
    /filter filt "Filter or block of filters" 
    /keep "Keep previous settings and results" 
    /only "Return only a single file, not a block." 
    /path "Return absolute path followed by relative files." 
    /save "Request file for saving, otherwise loading." 
    /local where data filt-names filt-values
][
    if none? out start-out 
    either file [

        either block? name [picked: copy name] [picked: reduce [to-file name]]
    ] [
        if not keep [picked: copy []]
    ] 
    if none? picked [picked: copy []] 
    if file: picked/1 [where: first split-path file] 
    while [not tail? picked] [
        set [name file] split-path first picked 
        either name <> where [remove picked] [
            change picked file 
            picked: next picked
        ]
    ] 
    picked: head picked 
    if any [not where not exists? where] [where: clean-path %.] 
    if not keep [
        fp/data: head fp/data 
        so/data: head so/data 
        si: 1
    ] 
    either filter [
        filters: either block? filt [filt] [reduce [filt]]
    ] [if any [not keep not block? filters] [pick-filter]] 
    ff/text: form filters 
    tt/text: either title [copy title-line] ["Select a File:"] 
    ob/text: either title [copy button-text] ["Select"] 
    if all [
        error? done: try [
            filt-names: copy head fp/data 
            filt-values: copy filter-list 
            either filter [
                insert head filt-names "Custom" 
                insert/only filt-values filters
            ] [
                filt-names: at filt-names index? fp/data
            ] 
            done: local-request-file data: reduce 

            [tt/text ob/text clean-path where picked filt-names filt-values found? 
            any [only] found? any [save]] 
            if done [
                dir-path: data/3 
                picked: data/4 

                if not filter [fp/data: at head fp/data index? data/5]
            ] 
            done
        ] 
        (get in disarm done 'code) = 328
    ] [
        done: false 
        read-dir/full either where [where] [dir-path] 
        show-pick 
        inform out 
        unfocus
    ] 
    if error? done [done] 
    if all [done picked any [path not empty? picked]] [
        either path [
            done: insert copy picked copy dir-path 
            either only [done/1] [head done]
        ] [
            foreach file picked [insert file dir-path] 
            either only [picked/1] [picked]
        ]
    ]
]
Group: Dialects ... Questions about how to create dialects [web-public]
Geomol:
27-Jul-2007
Example use of local variables. In line 70, 'a' is local, because 
it's a parameter to the procedure, 'b' is still global. After line 
80, 'b' also become local to the procedure. After returning from 
the procedure, both 'a' and 'b' are set back to their global values. 
In 'proctest', 'a' could have been called anything without changing 
the global 'a'.

>> do http://www.fys.ku.dk/~niclasen/rebol/bbcbasic.html
Script: "BBC BASIC" (27-Jul-2007)
BASIC v. 0.4.0 

>auto
   10 a=42
   20 b=1
   30 proctest(a)
   40 print "line 40 : a=";a " b=";b
   50 end
   60 def proctest(a)
   70 print "line 70 : a=";a " b=";b
   80 local b
   90 a=2:b=2
  100 print "line 100: a=";a " b=";b
  110 endproc
  120 0
>50end
>run
line 70 : a=        42 b=         1
line 100: a=         2 b=         2
line 40 : a=        42 b=         1
Group: !RebGUI ... A lightweight alternative to VID [web-public]
shadwolf:
29-Mar-2005
G4C TUT_MCListview

// ===========================================================
// A Multi Column (or Database) Listview..
// ===========================================================

WINDOW 126 90 367 373 "Listview"
	winattr style resize

xOnLoad
	// add some records to the listview & open..
	gosub #this AddRecords
	guiopen #this

xOnClose
	guiquit #this

// ===========================================================
// The listview
// - This is a normal MULTISELECT listview (the default).
// For this type to be triggered, you must double-click it.
// ===========================================================

XLISTVIEW 0 0 0 0 'The Title' "" var

	attr ID mylv
	attr resize 0022
	attr frame sunk

	// Give it a grid and allow the user to drag, drop & re-arrange
	// the lines - You can add more styles here..
	attr style grid/arrange/drag/drop/arrange

	// Add some columns. The first one we state with a '#'
	// in front to indicate we mean the 1st column.
	attr LVCOLUMN '#Item/width/120/TITLE/Description'

 attr LVCOLUMN 'Units/width/60/TITLE/Units/TYPE/number/JUSTIFY/RIGHT'

 attr LVCOLUMN 'Amount/width/60/TITLE/Amount/TYPE/number/JUSTIFY/RIGHT'

 attr LVCOLUMN 'Total/width/60/TITLE/Total/TYPE/number/JUSTIFY/RIGHT'

	// show the line double clicked..

 SetWinTitle #this 'SUM: $%Units x $%Amount = $($%Units * $%Amount)'


// ===========================================================
// This is a routine to add 100 records with various
// meaningless values to the above listview. Note how
// the fields can be used as normal variables.
// ===========================================================

xRoutine AddRecords
	local c

	use lv #this mylv

	// before we start, we HIDE the listview. This will
	// stop Gui4Cli from visually refreshing it every time
	// we add a record and will GREATLY increase the speed.
	// This will have no effect if the window is closed.
	// After we finish, we show it again..
	setevent #this mylv HIDE

	for c 0 100
		// add an empty record..
		lv add ''

		// Fill the fields with various values..
		%Item   = "This is Item $c"
		%Units  = $($c * 3)
		%Amount = $(($%Units / 2)*1000)
		%Total  = $($%Units * $%Amount)

	endfor

	// Show the listview again, refreshing the display..
	setevent #this mylv SHOW

// ===========================================================
// Right mouse button handling - Some menu choices..
// ===========================================================

xOnRMB


 QuickMenu -1 -1 'Select All/Remove selected/Add 100 records/#sepa/cancel'
	use lv #this mylv
	docase $$choice
		case = 0			// Select All
			lv select all
			break
		case = 1			// Remove selected
			lv delete selected
			break
		case = 2			// Add some records..
			gosub #this AddRecords
	endcase
Vincent:
9-Apr-2005
construct: func [
    block [block!] /with object [object!]
/local nb spec values name value
][
    if not with [object: object!]
    spec: copy []
    values: copy []
    parse/all :block [
        any [
            to set-word! (nb: 0) some [
                set name set-word! (nb: nb + 1 append spec :name)
            ]
            set value skip (

                insert tail values nb insert/only tail values :value
            )
        ]
    ]
    append spec none
    object: make object spec
    foreach [nb value] values [
        loop nb [
            set in object (to-word first spec) 

                either find [true false none on off] :value [do value][:value]
            spec: next spec
        ]
    ]
    object
]
Graham:
28-Jun-2006
I'm trying my way of creating templates, but have come across a problem 
with the check-group.  This takes, none, true and false as values, 
these are in the global context.  If I define more local words with 
the same names as these, how I can set them to the values in the 
global context?
Steeve:
11-Apr-2007
ladislav, i gan 1 second while intializing the dictionary with this 
train function:

train: func [
	words [block!]
	/local keys values pos i
][
	keys: to hash! unique words
	values: head insert/dup cp [] 0 2 * length? keys

 i: 0 loop length? keys [change values first keys values: skip values 
 2 keys: next keys]
	values: head values
	keys: head keys
	foreach word words [
			pos: 2 * index? find keys word 
			poke values pos 1 + pick values pos
	]
	sort/reverse/skip/compare values 2 2
	values
]
Group: Rebol School ... Rebol School [web-public]
Davide:
12-Mar-2010
I need a small function "my-compose" that takes a blocks, deep search 
tag! values and change it with the value of the word into the tag.
For example, if I have a test function like this:

x: 1
test: func [y /local z] [
	z: 3
	my-compose [ 
		print [ <x> + (<y> + <z>)]
	]
]

Calling:

test 2 

should return:

>> [print 1 + (2 + 3)]

My problem is to do the right bind in the parse:

my-compose: function [code [block!]] [elem rule pos] [
	rule: [any [

  pos: set elem tag! (change/only pos **magical-bind-here** to word! 
  to string! elem ) |		    	
		pos: any-block! :pos into rule |
		skip
	]]
	parse code rule
]
Steeve:
12-Mar-2010
you need to pass the values to map because the formula block only 
contains tag! which basically are strings (tags have no context, 
nor values).
if instead you use get-words as tags, you don't need to.

my-compose: func [code [block!] /local pos][
	parse code rule: [
		any [
			  to get-word! pos: (pos/1: get pos/1) skip 		    	
			| to any-block! into rule
		]
	]
	code
]

>>x: 1
>>y: 2
>>z: 3
>>my-compose [print :x + (:y + :z)]
==[print 1 + (2 + 3)]
Claude:
1-Jun-2010
REBOL[]


send: func [
	"Send a message to an address (or block of addresses)"
	;Note - will also be used with REBOL protocol later.
	address [email! block!] "An address or block of addresses"
	message "Text of message. First line is subject."
	/only   "Send only one message to multiple addresses"
	/header "Supply your own custom header"
	header-obj [object!] "The header to use"
	/attach "Attach file, files, or [.. [filename data]]"
	files [file! block!] "The files to attach to the message"
	/subject "Set the subject of the message"
	subj "The subject line"
	/show "Show all recipients in the TO field"
	/local smtp-port boundary make-boundary tmp from
][
	make-boundary: does []

	if file? files [files: reduce [files]] ; make it a block
	if email? address [address: reduce [address]] ; make it a block
	message: either string? message [copy message] [mold message]

	if not header [                 ; Clone system default header
		header-obj: make system/standard/email [

   subject: any [subj copy/part message any [find message newline 50]]
		]
	]
	if subject [header-obj/subject: subj]
	either none? header-obj/from [

  if none? header-obj/from: from: system/user/email [net-error "Email 
  header not set: no from address"]
		if all [string? system/user/name not empty? system/user/name][
			header-obj/from: rejoin [system/user/name " <" from ">"]
		]
	][
		from: header-obj/from
	]
	if none? header-obj/to [
		header-obj/to: tmp: make string! 20
		if show [
			foreach email address [repend tmp [email ", "]]
			clear back back tail tmp
		]
	]
	if none? header-obj/date [header-obj/date: to-idate now]

	if attach [

  boundary: rejoin ["--__REBOL--" system/product "--" system/version 
  "--" checksum form now/precise "__"]
		header-obj/MIME-Version: "1.0"

  header-obj/content-type: join "multipart/mixed; boundary=" [{"} skip 
  boundary 2 {"}]
		message: build-attach-body message files boundary
	]

	;-- Send as an SMTP batch or individually addressed:
	smtp-port: open [scheme: 'esmtp]
	either only [ ; Only one message to multiple addrs
		address: copy address
		; remove non-email values
		remove-each value address [not email? :value]

  message: head insert insert tail net-utils/export header-obj newline 
  message
		insert smtp-port reduce [from address message]
	] [
		foreach addr address [
			if email? addr [
				if not show [insert clear header-obj/to addr]

    tmp: head insert insert tail net-utils/export header-obj newline 
    message
				insert smtp-port reduce [from reduce [addr] tmp]
			]
		]
	]
	close smtp-port
]

resend: func [
	"Relay a message"
	to from message /local smtp-port
][
	smtp-port: open [scheme: 'esmtp]
	insert smtp-port reduce [from reduce [to] message]
	close smtp-port
]

build-attach-body: function [
	{Return an email body with attached files.}
	body [string!] {The message body}

 files [block!] {List of files to send [%file1.r [%file2.r "data"]]}
	boundary [string!] {The boundary divider}
][
	make-mime-header
	break-lines
	file
	val
][
	make-mime-header: func [file] [
		net-utils/export context [

   Content-Type: join {application/octet-stream; name="} [file {"}]
			Content-Transfer-Encoding: "base64"

   Content-Disposition: join {attachment; filename="} [file {"^/}]
		]
	]
	break-lines: func [mesg data /at num] [
		num: any [num 72]
		while [not tail? data] [
			append mesg join copy/part data num #"^/"
			data: skip data num
		]
		mesg
	]
	if not empty? files [
		insert body reduce [boundary "^/Content-type: text/html^/^/"]
		append body "^/^/"
		if not parse files [
			some [
				(file: none)
				[
					set file file! (val: read/binary file)
					| into [
						set file file!
						set val skip ;anything allowed
						to end
					]
				] (
					if file [
						repend body [
							boundary "^/"
							make-mime-header any [find/last/tail file #"/" file]
						]
						val: either any-string? val [val] [mold :val]
						break-lines body enbase val
					]
				)
			]
		] [net-error "Cannot parse file list."]
		append body join boundary "--^/"
	]
	body
]
Group: rebcode ... Rebcode discussion [web-public]
BrianH:
14-Oct-2005
(Thinking out loud) It occurs to me that computed branches would 
be a lot easier if you could reference the target values in your 
code, so that you have something to compute with. If the offsets 
were absolute you could just assign them to the label words (something 
that could be done in the first pass of the assembler rewrite of 
the branch statements). Relative offsets could be calculated pretty 
easily if you had something like a HERE opcode that would assign 
the current position to a variable that could be used soon afterwards 
to calculate the relative offset. For that matter, the HERE opcode 
could perform the assignment of the original label as well, and even 
be accomplished by a rewrite rule in the branch fixup pass of the 
assembler.


Here's my proposal for a HERE assembler directive. No native opcodes 
would need to be added - this would be another directive like label. 
This directive could be used to set the target values to words for 
later computation. Assuming BRAW stays relative and no absolute computed 
branch is added, it could also be used in computations to convert 
from absolute to relative offsets. This would be sufficient to make 
computed branches practical.


- A new directive HERE, taking two arguments, a word and a literal 
integer.

It would set the word to the position of the HERE directive, plus 
an offset specified in the second parameter. The offset would need 
to be a literal because the calculation would be performed ahead 
of time by the assembler - 0 would mean no offset. If you don't want 
to reset the position every time you branch to the word use an offset 
of 3. Resetting the word after every branch would allow its use as 
a temporary in absolute-to-relative calculations, but that would 
only be an advantage until the JIT or optimizer is implemented - 
the choice would be up to the developer. Having a mandatory second 
argument is necessary for reasons that will become clear later.


- The HERE directive would be rewritten away in the fix-bl function 
of the assembler like this:

REBOL []  ; So I could use SciTE to write this message

fix-bl: func [block /local labels here label] [
    labels: make block! 16
    block-action: :fix-bl
    if debug? [print "=== Fixing binding and labels... ==="]
    parse block [
        some [
            here:
            subblock-rule (here/1: bind here/1 words)
            |

            'label word! (here/1: bind here/1 words insert insert tail labels 
            here/2 index? here)
            |  ; Beginning of the added code
            'here word! integer! (

                here/1: bind 'set words  ; This is why HERE needs two arguments

                here/3: here/3 + index? here  ; Offset from position of this directive
                if (here/3 < 1) or (here/3 > 1 + length? block) [
                    error/with here "Offset out of bounds:"
                ]
            )  ; End of the added code
            |
            opcode-rule (here/1: bind here/1 words)
            |
            skip (error here)
        ]
    ]
    parse block [
        some [
            here:
            ['bra word! | 'brat word! | 'braf word!] (

                if not label: select labels here/2 [error/with here "Missing label:"]
                here/2: label - index? here
            )
            |
            opcode-rule
            |
            skip (error here)
        ]
    ]
]
Oldes:
18-Oct-2005
ints-to-sbs: func[

 ints [block!]	 "Block of integers, that I want to convert to SBs"

 /complete l-bits "Completes the bit-stream => l-bits stores the nBits 
 info of the values"
	;/maxb mb
	/local b b2 l bits sb
][
	ints: reduce ints
	max-bits: 0
	bits: make block! length? ints
	foreach i ints [
		;b: enbase/base load rejoin ["#{" to-hex i "}"] 2
		b: enbase/base head reverse int-to-ui32 i 2
		b: find b either i < 0 [#"0"][#"1"]
		b: copy either none? b [either i >= 0 ["00"]["11"]][back b]
		;insert b either i >= 0 [#"0"][#"1"]
		if max-bits < l: length? b [max-bits: l]
		append bits b
	]
	foreach b bits [
		if max-bits > l: length? b [
			insert/dup b b/1 max-bits - l
		]
	]
	either complete [
		sb: int-to-bits max-bits l-bits
		foreach b bits [insert tail sb b]
		sb
	][	
		bits
	]
]

int-to-FB: func[i /local x y fb][
	x: to integer! i
	y: to integer! (either x = 0 [i][i // x]) * 65535

 fb: rejoin [either x = 0 ["0"][first ints-to-sbs to block! x] int-to-bits 
 y 16]
	if all [x = 0 i < 0][fb/1: #"1"]
	fb
]
BrianH:
4-Nov-2005
Here are some initial comments on the recently posted rebcode documentation 
draft:

- It has been suggested on the list that since the assembler's rewrite 
engine is a mezzanine, it might not be included in the final version, 
in favor of (to promote?) user-made rewrite engines. If not, you 
would need to change the documentation to match, especially section 
1.4.

- It needs to be made clear somewhere in the initial description 
of the rebcode dialect that rebcode is a statement-based language, 
not an expression-based language like the do dialect. Opcodes perform 
actions, but don't return anything per-se. The 2.1 or 2.3 sections 
would be a good place for this explanation to be.

- In the "Branches are always relative" note at the end of 2.6, there 
is a sentence "The branches are always relative to the current block." 
that could be removed. The whole note should probably be renamed 
to "Branches are always local" because the note doesn't really cover 
that they are also relative. Also the phrase "use a branch opcode 
to" could be replaced with "branch to" and be less awkward.

- A common mistake in specifying literal branch offsets is to miscalculate 
what location the offsets are relative to. This mistake would be 
less likely if the third paragraph of 2.8 were changed to "The argument 
to the branch opcodes is an integer value, representing how much 
of an offset you want the branch to perform. Branch offsets are always 
relative to the location after the branch statement, not the absolute 
offset within the block. Positive values branch forward; negative, 
backward. The branch target must always fall within the current code 
block." as this is the actual branch behavior (and more clear).

- The sentence in 2.8 "The brab opcode allows computed branch offsets 
to be created." isn't really true right now, at least in any practical 
way. The current behavior is more like "The brab opcode allows you 
to branch to an offset selected at runtime by an index.".

- The paragraph at the end of 2.8 "There is also a special case of 
operation. If the block argument to BRAB is an integer (created from 
a label), then the branch is made to that relative location plus 
the value of the index argument." would be a good idea to be implemented 
(I've submitted it to RAMBO), but is rather awkwardly phrased. This 
could be rephrased once the behavior is implemented, or left alone 
if you don't want most rebcode users to use this behavior.

- In section 2.9, the sentence "Result then refers to the value returned 
from the function." may be better said as "The word result is then 
assigned the value returned from the function.".

- 4.1.*: The phrasing of many of these entries is awkward. Also, 
remember that opcodes don't return anything, they modify operands.

- 4.1.1: I'm not sure "integral" means "the integer part of" as it 
is used here; the word may be more related to integrate than integer.

- 4.1.4: Lowercase the "Tail" word to be consistent. Otherwise, well 
phrased.

- 4.1.5: The descriptions of change, copy and insert don't describe 
how their amount parameter is used. You could describe change as 
"Changes part of a series at the current position to that part of 
a value (-1 for the whole value).", copy as "Set the operand to a 
partial copy of the series (-1 for all) from the current position.", 
and insert as "Inserts part of one series (-1 for all) into another 
at the current position.". Or, you could provide further explanation 
in some new 2.* section.

- 4.1.6: In the description of index?, change "Returns the" to "Set 
the operand to".

- 4.1.7: Does not reflect the renaming of the opcode get to getw 
and the addition of setw. Also, instances of "Result modified" should 
be changed to "Set result" or "Set operand to result".
- 4.3.3: The braw opcode has been removed.
Group: SQLite ... C library embeddable DB [web-public].
Ashley:
8-May-2006
It all depends.


 250,000 integers vs 250,000 multi-column rows with large string values
	client device with 16MB RAM vs 'server' with 4GB RAM
	'local' query vs pulling the data over a network


There are plenty of optimization strategies. One technique, if concurrency 
is not an issue for you, is to have your query return 250,000 rowids 
then page through rows based on simple 'rowid in (...)' type queries. 
It's fast and efficient, but not too great if others are modifying 
the same table(s) at the same time.
Group: !REBOL3-OLD1 ... [web-public]
Pekr:
6-Apr-2006
btw - are modules part of rebol 3.0? I am not sure, maybe a wrong 
idea, but with modules, you define 'export: [a b c] values, or import, 
or local and you have also 'options .... (and sorry if I got it wrong 
and if it would mean lots of incompatibilities), so, couldn't functions 
use similar technique? :-)
Maxim:
6-Apr-2006
values defined as default arguments are just like /local variables 
in rebol, they are only defined once and persist from one function 
call to another.  thus blocks get re-used.  We get used to this in 
rebol and do our own copy []   and   return first reduce [val val:none]
Gregg:
14-Apr-2006
The current library interface is servicable, but could be improved.

For example, char arrays in C structs are a real pain to deal with,

there is redundancy if you're importing a number of routines, from 

the same library, and extra work is required to deal with pointers
to values.

The biggest issue for me seems to be that I have to use COMPOSE 

heavily to get the results I want, or there's a lot of duplication
in struct and routine defs.

--- Easier Routine Declarations

The only thing I've addressed in my lib interface dialect is
making it easier to declare routines. I posted it to REBOl.org
for discussion:


http://www.rebol.org/cgi-bin/cgiwrap/rebol/view-script.r?script=lib-dialect.r

--- Pointers


This is maybe a bit of an extreme example, but I had to do it, so 
it's not purely theoretical.

    LPINT-def: [value [integer!]] none

    LPINT: make struct! LPINT-def none

This struct shows where a nested struct is needed.

    _FAX_JOB_PARAM-def: compose/deep/only [

        SizeOfStruct    [integer!]      ; DWORD structure size, in bytes

        RecipientNumber [string!]       ; LPCTSTR   pointer to recipient's 
        fax number

        RecipientName   [string!]       ; LPCTSTR   pointer to recipient's 
        name

        Tsid            [string!]       ; LPCTSTR   pointer to transmitting 
        station identifier

        SenderName      [string!]       ; LPCTSTR   pointer to sender's name

        ;SenderName      [struct! [value [string!]]]       ; LPCTSTR   pointer 
        to sender's name

        SenderCompany   [string!]       ; LPCTSTR   pointer to sender's company

        SenderDept      [string!]       ; LPCTSTR   pointer to sender's department

        BillingCode     [string!]       ; LPCTSTR   pointer to billing code

        ScheduleAction  [integer!]      ; DWORD job scheduling action code

        ;ScheduleTime    [struct! (SYSTEMTIME-def)]        ; SYSTEMTIME  
          time to send fax
            wYear       [short]
            wMonth      [short]
            wDayOfWeek  [short]
            wDay        [short]
            wHour       [short]
            wMinute     [short]
            wSecond     [short]
            wMilliseconds [short]

        DeliveryReportType  [integer!]      ; DWORD e-mail delivery report 
        type

        DeliveryReportAddress [string!]     ; LPCTSTR   pointer to e-mail 
        address

        DocumentName        [string!]       ; LPCTSTR   pointer to document 
        name to display
        CallHandle          [integer!]      ; HCALL reserved
        ;_PTR   Reserved[3]  [integer!]      ; DWORD must be zero
        _PTR-0  [integer!]      ; DWORD must be zero
        _PTR-1  [integer!]      ; DWORD must be zero
        _PTR-2  [integer!]      ; DWORD must be zero
        _PTR-3  [integer!]      ; DWORD must be zero
    ]
    _FAX_JOB_PARAM: make struct! _FAX_JOB_PARAM-def none
    _FAX_JOB_PARAM/SizeOfStruct: length? third _FAX_JOB_PARAM

    fax-complete-job-params: make routine! compose/deep/only [

        JobParams     [struct! (LPINT-def)] ; ptr to job information struct

        CoverPageInfo [struct! (LPINT-def)] ; ptr to cover page struct
        return:       [integer!]
    ] winfax.dll "FaxCompleteJobParamsA"

So, the API call returns pointers to structs containing the 
data we want; to get it we need to dereference the pointers
after the call.

    complete-job-params: func [
        /local
            params-ptr cover-ptr    ; API return pointers

            params cover            ; REBOL structs with data from API
    ][
        ; allocate return pointer structs for API call
        params-ptr: make-LPINT
        cover-ptr: make-LPINT

        ; make the API call

        reduce either 0 <> fax-complete-job-params params-ptr cover-ptr [

            ; get data from pointers returned by the API

            params: get-dereferenced-data params-ptr _FAX_JOB_PARAM-def

            cover:  get-dereferenced-data cover-ptr  _FAX_COVERPAGE_INFO-def
        ...



Getting the de-ref'd data is the real pain, and seems like it might
be unsafe in the way I did it, though it worked.

    get-dereferenced-data: func [

        {Given a pointer to memory, copy the target data into a REBOL struct.}

        pointer [struct!]   "LPINT structure whose /value is the data pointer"

        struct-def [block!] "The struct you want returned with data"
        /local struct data orig-pointer result
    ] [

        struct: make struct! compose/deep/only [ ; make wrapper struct
            sub [struct! (struct-def)]
        ] none

        orig-pointer: third struct              ; store original inner pointer

        change third struct third pointer       ; change inner pointer to 
        ref'd data

        data: copy third struct/sub             ; copy data from the inner 
        struct

        change third struct orig-pointer        ; restore inner pointer

        result: make struct! struct-def none    ; make result struct

        change third result data                ; change data in result struct
        struct: data: orig-pointer: none
        result
    ]


--- char arrays in structs, or as routine parameters

You can't just declare a fixed size block or string to do this, 
you have to (AFAIK), have individual elements for each item.
That's a huge pain if you have a 128 element array, so I end
up generating them dynamically. I think that was Cyphre's idea
originally, but I don't have notes on it.

    make-elements: func [name count type /local result][
        if not word? type [type: type?/word type]
        result: copy "^/"
        repeat i count [
            append result join name [i " [" type "]" newline]
        ]
        to block! result
    ]

    GUID: make struct! GUID-def: compose [
        Data1   [integer!]  ; unsigned long
        Data2   [short]     ; unsigned short
        Data3   [short]     ; unsigned short
        (make-elements 'Data4 8 #"@")  ; unsigned char
    ] none


--- MAKE-ing structs

How do other people make structs from prototypes? 

    make-struct: func [prototype /copy /with data] [
        make struct! prototype either copy
            [second prototype]
            [either with [reduce [data]][none]]
    ]


--- BSTR type

I've only needed it for one project, but it might be worth 
finding out if it would be worth adding BSTR support for
Windows, as a routine datatype.
BrianH:
1-May-2006
;The changed build function would be:
build: func [
    {Build a block using given values}
    block [block! paren! path! lit-path! set-path!]
    /with
    values [block!]
    /local context inner
] [
    values: any [values [only: :encl ins: :dtto]]
    context: make object! values
    inner: func [block /local item item' pos result] [
        result: make :block length? :block
        parse block [
            any [
                pos: set item word! (

                    either all [item': in context item item <> 'self] [
                        change pos item'
                        set/any [item pos] do/next pos
                        insert tail :result get/any 'item
                    ] [insert tail :result item pos: next pos]
                ) :pos | set item get-word! (

                    either all [item': in context item item <> 'self] [
                        insert/only tail :result get/any item'
                    ] [insert tail :result item]
                ) | set item [

                    block! | paren! | path! | set-path! | lit-path!
                ] (
                    insert/only tail :result inner :item

                ) | set item skip (insert/only tail :result get/any 'item)
            ]
        ]
        :result
    ]
    inner :block
]
BrianH:
1-Sep-2006
conjoin: func [
    "Join the values in a block together with a delimiter."
    data [any-block!] "The series to join"
    delimiter "The value to put into the series"
    /only "Inserts a series delimiter as a series."
    /quoted "Puts string values in quotes."
    /local
] [
    if empty? data [return make data 0]

    local: either series? local: first data [copy local] [form local]

    while [not empty? data: next data] either any-string? local [pick 
    [

        [local: insert local reduce [delimiter {"} first data {"}]]
        [local: insert insert local delimiter first data]
    ] quoted] [pick [
        [local: insert insert/only local delimiter first data]
        [local: insert insert local delimiter first data]
    ] only]
    head local
]
Anton:
6-Sep-2006
; Brian H's version corrected by Anton:
; - LOCAL starts at its tail
; - PICK converted to EITHER (PICK doesn't work with NONE)
; - /QUOTED applied to first value
conjoin: func [

 "Join the values in a block together with a delimiting PAD value."
	data [any-block!] "The series to join"
	pad "The value to put into the series"
	/only "Inserts a series PAD as a series."
	/quoted "Puts string values in quotes."
	/local ; <- used to track tail of the result as we build it
] [
	if empty? data [return make data 0]

 local: tail either series? local: first data [copy local] [form :local]

 if all [quoted any-string? local][local: insert tail insert head 
 local {"} {"}] ; quote the first value
	; <- (local should be at its tail at this point)
	while [not empty? data: next data] either any-string? local [
		either quoted [

   [local: insert insert insert insert local pad {"} first data {"}]
		][
			[local: insert insert local pad first data]
		]
	] [
		either only [
			[local: insert insert/only local pad first data]
		][
			[local: insert insert local pad first data]
		]
	]
	head local
]

; test
conjoin [] ""
conjoin [] ","
conjoin [1 2 3] '|
conjoin [[1] 2 3] '|
conjoin ["one" 2 3] ", "
conjoin [["one"] 2 3] '|
conjoin [1 2 [3]] [pad]
conjoin [[1] 2 [3]] [pad]
conjoin/only [[1] 2 [3]] [pad]
conjoin/only [[1] 2 [3]] 'pad

conjoin/quoted [1 2 3] '|
conjoin/quoted [[1] 2 3] '|
conjoin ["one" 2 3] ", "
conjoin [1 2 [3]] [pad]
conjoin/only [1 2 [3]] [pad]
conjoin/only [1 2 [3]] 'pad
Anton:
6-Sep-2006
; Anton's enhanced version:
; - /quote is applied to first value, if a string

; - reorders PAD and DATA arguments so PAD is first (being likely 
always short)
; - distinguishes /only and /pad-only
; - renames /quoted -> /quote
conjoin: func [

 "Join the values in a block together with a delimiting PAD value."
	pad "The value to put into the series"
	data [any-block!] "The series to join"
	/only "Inserts a series value in DATA as a series."

 /pad-only "Inserts a series PAD as a series." ; <-- this might not 
 be used much in practice (easy to add extra brackets around PAD)
	/quote "Puts string values in quotes."
	/local ; <- used to track tail of the result as we build it
] [
	if empty? data [return make data 0]

 local: tail either series? local: first data [copy local] [form :local]

 if all [quote any-string? local][local: insert tail insert head local 
 {"} {"}] ; quote the first value
	; <- (local should be at its tail at this point)
	while [not empty? data: next data] either any-string? local [
		either quote [

   [local: insert insert insert insert local pad {"} first data {"}]
		][
			[local: insert insert local pad first data]
		]
	] [
		either only [
			either pad-only [
				[local: insert/only insert/only local pad first data]
			][
				[local: insert/only insert local pad first data]
			]
		][
			either pad-only [
				[local: insert insert/only local pad first data]
			][
				[local: insert insert local pad first data]
			]
		]
	]
	head local
]

; test
conjoin "" []
conjoin "," []
conjoin '| [1 2 [3]]
conjoin '| [[1] 2 [3]]
conjoin ", " [{one} 2 [3]]
conjoin '| [["one"] 2 [3]]
conjoin/only '| [["one"] 2 [3]]

conjoin/only [pad] [1 2 [3]] ; ONLY and PAD-ONLY make no difference 
in string mode
conjoin/only [pad] [[1] 2 [3]]

conjoin/pad-only [pad] [1 2 [3]] ; ONLY and PAD-ONLY make no difference 
in string mode
conjoin/pad-only [pad] [[1] 2 [3]]

conjoin/only/pad-only [pad] [1 2 [3]] ; ONLY and PAD-ONLY make no 
difference in string mode
conjoin/only/pad-only [pad] [[1] 2 [3]]

conjoin/quote "" []
conjoin/quote "," []
conjoin/quote '| [1 2 [3]]
conjoin/quote '| [[1] 2 [3]] ; QUOTE doesn't work in block mode
conjoin/quote ", " [{one} 2 [3]]
conjoin/quote '| [["one"] 2 [3]]
conjoin/quote/only '| [["one"] 2 [3]]

conjoin/quote/only [pad] [1 2 [3]] ; ONLY and PAD-ONLY make no difference 
in string mode
conjoin/quote/only [pad] [[1] 2 [3]]

conjoin/quote/pad-only [pad] [1 2 [3]] ; ONLY and PAD-ONLY make no 
difference in string mode
conjoin/quote/pad-only [pad] [[1] 2 [3]]

conjoin/quote/only/pad-only [pad] [1 2 [3]] ; ONLY and PAD-ONLY make 
no difference in string mode
conjoin/quote/only/pad-only [pad] [[1] 2 [3]]
BrianH:
7-Sep-2006
delimit: func [
    "Put a value between the values in a series."
    data [series!] "The series to delimit"
    delimiter "The value to put into the series"
    /only "Inserts a series delimiter as a series."
    /copy "Change a copy of the series instead."
    /local
] [
    while either copy [
        if empty? data [return make data 0]
        local: make data 2 * length? data
        [
            local: insert/only local first data
            not empty? data: next data
        ]
    ] [
        local: data
        [not empty? local: next local]
    ] pick [
        [local: insert local delimiter]
        [local: insert/only local delimiter]
    ] none? only
    head local
]

conjoin: func [
    "Join the values in a block together with a delimiter."
    data [any-block!] "The series to join"
    delimiter "The value to put into the series"
    /only "Inserts a series delimiter as a series."
    /quoted "Puts string values in quotes."
    /local
] [
    if empty? data [return make data 0]

    local: tail either series? local: first data [copy local] [form :local]
    while [not empty? data: next data] either any-string? local [
        either quoted [
            local: insert tail insert head local {"} {"}

            [local: insert insert insert insert local delimiter {"} first data 
            {"}]
        ] [[local: insert insert local delimiter first data]]
    ] [pick [
        [local: insert insert local delimiter first data]
        [local: insert insert/only local delimiter first data]
    ] none? only]
    head local
]
BrianH:
7-Sep-2006
conjoin: func [
    "Join the values in a block together with a delimiter."
    data [any-block!] "The series to join"
    delimiter "The value to put into the series"
    /only "Inserts a series delimiter as a series."
    /quoted "Puts string values in quotes."
    /local
] [
    if empty? data [return make data 0]

    local: tail either series? local: first data [copy local] [form :local]
    while [not empty? data: next data] either any-string? local [
        either quoted [
            local: insert tail insert head local {"} {"}

            [local: insert insert insert insert local (delimiter) {"} first data 
            {"}]
        ] [[local: insert insert local (delimiter) first data]]
    ] [pick [
        [local: insert insert local (delimiter) first data]
        [local: insert insert/only local (delimiter) first data]
    ] none? only]
    head local
]
BrianH:
10-Sep-2006
delimit: func [
    "Put a value between the values in a series."
    data [series!] "The series to delimit"
    delimiter "The value to put into the series"
    /only "Inserts a series delimiter as a series."
    /copy "Change a copy of the series instead."
    /local
] [
    while either copy [
        if empty? data [return make data 0]
        local: make data 2 * length? data
        [
            local: insert/only local first data
            not empty? data: next data
        ]
    ] [
        local: data
        [not empty? local: next local]
    ] either only [
        [local: insert/only local delimiter]
    ] [[local: insert local delimiter]]
    head local
]

conjoin: func [
    "Join the values in a block together with a delimiter."
    data [any-block!] "The values to join"
    delimiter "The value to put in between the above values"
    /only "Inserts a series delimiter as a series."
    /quoted "Puts string values in quotes."
    /local
] [
    if empty? data [return make data 0]

    local: tail either series? local: first data [copy local] [form :local]
    while [not empty? data: next data] either any-string? local [
        either quoted [
            local: insert tail insert head local {"} {"}

            [local: insert insert insert insert local (delimiter) {"} first data 
            {"}]
        ] [[local: insert insert local (delimiter) first data]]
    ] [
        either only [

            [local: insert insert/only local (delimiter) first data]
        ] [[local: insert insert local (delimiter) first data]]
    ]
    head local
]
Anton:
24-Nov-2006
Functions like these are very useful to have. I could have used them 
recently while doing file searching.
However, I wouldn't like to see these functions included as is.

- Not very efficient. That's ok  for searching small strings or the 
contents of short files, but bad when searching large files for many 
strings. 

- Not generic. The name suggests many datatypes are supported. Better 
names might be find-any-string, find-all-strings
- The above FINDALL does not keep FINDIT as a local.

- The argument names are too short, so they are not distinct or descriptive 
enough.

- The return values are not defined clearly in the function doc strings. 
The above issues are fixable, but it will take some time.
BrianH:
21-Sep-2007
Rebolinth, the main difference between functions in R2 and R3 is 
that the values assigned to the local variables in an R3 functions 
are freed after the function returns, while that is not the case 
in R2. Oh, and they're faster.
btiffin:
9-Oct-2007
format: make function! [
    "Format a string according to the format dialect."
    rules {A block in the format dialect. E.g. [10 -10 #"-" 4]}
    values
    /pad p
    /local out val
][
    p: any [p #" "]
    unless block? rules [rules: reduce [rules]]
    unless block? values [values: reduce [values]]
    val: 0
    foreach item rules [
        if word? item [item: get item]
        val: val + switch/default type?/word item [
            integer! [abs item]
            string! [length? item]
            char! [1]
        ] [0]
    ]
    out: make string! val
    insert/dup out p val
    foreach rule rules [
        if word? rule [rule: get rule]
        switch type?/word rule [
            integer! [
                val: first values
                values: next values
                pad: rule
                if negative? rule [
                    val: form :val
                    rule: negate rule
                    pad: rule - length? val
                    if positive? pad [out: skip out pad]
                    pad: length? val
                ]
                change out val
                out: skip out pad
            ]
            string! [out: change out rule]
            char! [out: change out rule]
        ]
    ]
    if not tail? values [append out values]
    head out
]

printf: make function! [
    "Formatted print."
    fmt "Format"
    val "Value or block of values"
][
    print format fmt val
]
BrianH:
31-Oct-2008
The SET [a: b:] issue:


In current REBOL a block argument to SET for the word parameter needs 
to be filled with values of the word! type, not any other word types. 
This is in contrast with just passing a single word value, which 
can be of any word type. The result of this is that values in word 
block argument need to be converted to word! before being passed 
as a parameter.


There is no reason that those words need to be of the word! type, 
particularly since the block isn't evaluated. One interesting thing 
about REBOL is that a word! bound to a context will refer to the 
same value as words of the other word types with the same spelling 
otherwise. This means that if you set a lit-word! to a value, you 
can get the same value from the corresponding word! or get-word!.


Now, the current behavior has some bad consequences as well, usage 
issues. Some word block arguments had to be preprocessed. The big 
problem we ran into was when we wanted to use SET inside of functions 
created with the new FUNCT function (temporary name).


FUNCT collects set-words from the code block and nested blocks and 
adds them to the function's locals. This is cool and works a lot 
better than I thought it would, and all action handlers of the new 
GUI are turned into functions with FUNCT. The problem we ran into 
is that when we had to use block SET to set local variables, those 
local variables would leak into an outer scope, wreaking havoc. If 
you are specifying the function spec directly there is an easy workaround 
for that: Just specify the locals that aren't caught with your own 
/local vars. However, you can't specify the function spec of an action 
handler in the dialect, just the code blocks (security/stability). 
This meant that if you wanted locals to be captured, you had to set 
them to none earlier in the function. How soon do you expect that 
to fail?


An easy solution to this would be to specify the words you want local 
as set-word! values in the literal word block argument to the SET. 
Those set-words would then be caught by FUNCT without any overhead. 
No problem, except for that SET doesn't allow it right now.


This is what we are going to fix in the next build: You will be able 
to do SET [a: b:] value.
BrianH:
7-Jan-2009
Here's the current source for LOAD:

load: func [
	{Loads a file, URL, or string.}
	source [file! url! string! any-block! binary!]

 /header  {Includes REBOL header object if present. Preempts /all.}

;	/next    {Load the next value only. Return block with value and 
new position.}

;	/library {Force file to be a dynamic library. (Command version)}
;	/markup  {Convert HTML and XML to a block of tags and strings.}
	/all     {Load all values. Does not evaluate REBOL header.}
	/unbound {Do not bind the block.}
	/local data tmp
][
	; Note: Avoid use of ALL func, because of /all option
	if any-block? :source [return :source]

	data: case [
		string? source [to-binary source]
		binary? source [source]
		; Check for special media load cases: (temporary code)
		find [%.jpg %.jpeg %.jpe] suffix? source [
			return load-jpeg read/binary source
		]

  url? source [read source] ; can this possibly return not binary! 
  ?
		file? source [read source] ; binary! or block of file!
	]

 ; At this point, data is binary!, a block of file!, or something 
 weird.

	if binary? :data [
		unless find [0 8] tmp: utf? data [
			cause-error 'script 'no-decode ajoin ['UTF tmp]
		]

		; Only load script data:
		if any [header not all] [ ; Note: refinement /all
			if tmp: script? data [data: tmp]
		]
	]

	unless block? :data [data: to block! :data] ; reduce overhead

 ; data is a block! here, unless something really weird is going on
	tmp: none
	
	; Is there a REBOL script header:
	if any [header not all] [ ; /header preempts /all
		tmp: unless any [

   ;not any [file? source url? source] ; removed: hdr in string is same
			unset? first data ; because <> doesn't work with unset!
			'rebol <> first data
			not block? second data
		][ ; Process header:
			attempt [construct/with second data system/standard/script]
		]
		; tmp is header object or none here
		case [
			tmp [
				remove data
				either header [change data tmp][remove data]
				tmp: tmp/type = 'module ; tmp true if module
			]
			header [cause-error 'syntax 'no-header data]
		]
	]
	; tmp is true if module, false or none if not

 ; data is a block!, with possible header object in first position

	; Bind to current global context if not a module:
	unless any [
		unbound
		tmp ; not a module
	][
		bind/new data system/contexts/current
	]

 ; data is a block! here, unless something really weird is going on

	; If appropriate and possible, return singular data value:
	unless any [ ; avoid use of ALL
		all
		header ; This fixes a design flaw in R2's LOAD
		;not block? :data ; can this ever happen?
		empty? data ; R2 compatibility
		not tail? next data
	][data: first data]
	; If /all or /header, data is a block here

	:data
]
BrianH:
12-Feb-2009
; Here's a version of COLLECT without /into, a typical example of 
a builder.
collect: func [

 "Evaluates a block, storing values via KEEP function, and returns 
 block of collected values."
	body [block!] "Block to evaluate"
	/local output
][
	output: make block! 16
	do func [keep] body func [value /only] [
		apply :append [output :value none none only]
		:value
	]
	output
]

; Here's COLLECT with the /into option.
collect: func [

 "Evaluates a block, storing values via KEEP function, and returns 
 block of collected values."
	body [block!] "Block to evaluate"
	/into "Collect into a given series, rather than a new block"
	output [series!] "The series to output to"
][
	unless output [output: make block! 16]
	do func [keep] body func [value /only] [
		output: apply :insert [output :value none none only]
		:value
	]
	either into [output] [head output]
]


Note that the version with /into also lets you use other series types 
than just block!. This option added to REDUCE and COMPOSE would let 
you create parens and paths as well, even though REDUCE and COMPOSE 
can only take block! specs.
Steeve:
12-Mar-2009
i make a proposal:

Most of the times, we use the same rules several times on different 
data.
reword should be able to not reconstruct the rules if so.

I used the similiar tricks in some scripts, for example:

map-chars: func [
	{replace/all pair chars in a string}
	data [string!] values [block!]
	/local chars pos
][

 ;** if the first value in values is a bitset, do not reconstruct 
 the bitset
	unless bitset? chars: first values [
		chars: make bitset! 256
		forskip array 2 [append chars array/1]
		insert values chars
	]
	pos: data
	values: next values

 while [pos: find pos chars][pos: change/part pos select/skip values 
 first pos 2 1]
	data
]

data: "Hello You"
map-chars copy data values: [#"s" "SS" #"t" #"T"] 
;** the second call is faster
map-chars copy data values
Ammon:
25-Mar-2009
Instead of having to add a lot of refinements and refinement handling 
to related functions you can just put them together into an object 
and as each of the functions are called they are setting values that 
are local to the collection of functions and each of those values 
modify the behavior as if you were using refinements and passing 
in more values.  It's kind of an inference engine.
BrianH:
3-Apr-2009
load: func [
	{Loads a file, URL, or string.}

 source [file! url! string! binary! block!] {Source or block of sources}

 /header  {Includes REBOL header object if present. Preempts /all.}

 /next    {Load the next value only. Return block with value and new 
 position.}

;	/library {Force file to be a dynamic library. (Command version)}
;	/markup  {Convert HTML and XML to a block of tags and strings.}
	/all     {Load all values. Does not evaluate REBOL header.}
	/unbound {Do not bind the block.}
	/local data content val rst tmp

][  ; Note: Avoid use of ALL and NEXT funcs, because of /all and 
/next options
	content: val: rst: tmp: none ; In case people call LOAD/local
	
	; Retrieve the script data
	data: case [
		block? source [ ; Load all in block
			return map x source [apply :load [:x header next all unbound]]
		]
		string? source [source] ; Will convert to binary! later
		binary? source [source]
		; Otherwise source is file or url
		'else [
			; See if a codec exists for this file type
			tmp: find find system/catalog/file-types suffix? source word!
			; Get the data, script required if /header
			content: read source  ; Must be a value, not unset
			case [
				binary? :content [content] ; Assumed script or decodable
				string? :content [content] ; Assumed script or decodable
				header [cause-error 'syntax 'no-header source]
				block? :content [content]
				'else [content: reduce [:content]]
			] ; Don't LOAD/header non-script data from urls and files.

  ] ; content is data if content doesn't need copying, or none if it 
  does
	]
	;print [1 "data type?" type? :data 'content true? :content]
	if string? :data [data: to-binary data] ; REBOL script is UTF-8

 assert/type [data [binary! block!] content [binary! string! block! 
 none!]]
	assert [any [binary? :data not header]]
	if tmp [ ; Use a codec if found earlier
		set/any 'data decode first tmp :data

  ; See if we can shortcut return the value, or fake a script if we 
  can't
		case [

   block? :data [if header [insert data val: make system/standard/script 
   []]]

   header [data: reduce [val: make system/standard/script [] :data]]

   (to logic! unbound) and not next [return :data] ; Shortcut return

   any [next any-block? :data any-word? :data] [data: reduce [:data]]
			'else [return :data] ; No binding needed, shortcut return
		]
		assert/type [data block!] ; If we get this far
	]
	;print [2 'data mold to-string :data]
	
	if binary? :data [ ; It's a script
		unless find [0 8] tmp: utf? data [ ; Not UTF-8
			cause-error 'script 'no-decode ajoin ["UTF-" abs tmp]
		]
		; Process the header if necessary
		either any [header not all] [
			if tmp: script? data [data: tmp] ; Load script data
			; Check for a REBOL header
			set/any [val rst] transcode/only data
			unless case [
				:val = [rebol] [ ; Possible script-in-a-block
					set/any [val rst] transcode/next/error rst
					if block? :val [ ; Is script-in-a-block
						data: first transcode/next data
						rst: skip data 2
					] ; If true, val is header spec
				]
				:val = 'rebol [ ; Possible REBOL header
					set/any [val rst] transcode/next/error rst
					block? :val ; If true, val is header spec
				]
			] [ ; No REBOL header, use default
				val: [] rst: data
			]
			; val is the header spec block, rst the position afterwards

   assert/type [val block! rst [binary! block!] data [binary! block!]]
			assert [same? head data head rst]
			; Make the header object

   either val: attempt [construct/with :val system/standard/script] 
   [
				if (select val 'content) = true [
					val/content: any [:content copy source]
				]
			] [cause-error 'syntax 'no-header data]
			; val is correct header object! here, or you don't get here
			; Convert the rest of the data if necessary and not /next
			unless any [next block? data] [data: rst: to block! rst]
			if block? data [ ; Script-in-a-block or not /next
				case [

     header [change/part data val rst] ; Replace the header with the object

     not all [remove/part data rst]	; Remove the header from the data
				]
				rst: none ; Determined later
			]
		] [rst: data] ; /all and not /header
	]

 ; val is the header object or none, rst is the binary position after 
 or none

 assert/type [val [object! none!] rst [binary! none!] data [binary! 
 block!]]

 assert [any [none? rst same? head data head rst] any [val not header]]

 ;print [3 'val mold/all :val 'data mold/all :data "type?" type? :data]
	
	; LOAD/next or convert data to block - block either way
	assert [block? data: case [
		not next [ ; Not /next
			unless any [block? data not binary? rst] [data: to block! rst]
			data
		]
		; Otherwise /next

  block? data [reduce pick [[data] [first+ data data]] empty? data]
		header [reduce [val rst]] ; Already transcoded above
		binary? rst [transcode/next rst]
	]]
	
	; Bind to current global context if not a module
	unless any [ ; Note: NOT ANY instead of ALL because of /all
		unbound
		(select val 'type) = 'module
	][
		bind/new data system/contexts/current
	]
	;print [6 'data mold/all :data 'tmp mold/all :tmp]
	
	; If appropriate and possible, return singular data value
	unless any [
		all header next  ; /all /header /next
		empty? data
		1 < length? data
	][set/any 'data first data]
	;print [7 'data mold/all :data]
	
	:data
]
Janko:
3-Jun-2009
BrianH: I won't be able to make an example (and maybe it's not possible 
now) .. but I was asking because of this maybe stupid idea for "poor 
mans continuations" 


that are built on top of language as a lib (because of rebol powerfull 
treatng of itself): 


- you have a function >>myfunc: func [ a /local b ] [ b: 5  return-cont 
 a + b ] <<
- at runtime when function is called you reach return-cont which:

  - you collect all it's local words and their values into a block 
  (with stack/args etc)  for example [ a 1 b 5  ]

  - you also get current position of a running block (already seems 
  to be possible with stack/block + next )  [ a + b ]

  - you generate a function at runtime that has >>does [a: 1 b: 5 a 
  + b ]<<
  - you return taht function as a normal return value.
this means that this would be possible:
>> cont: myfunc 5
... do some stuff
>> cont 
== 6

any thoughts :) ?
Geomol:
6-Oct-2009
Does function! and closure! work backwards when dealing with indirect 
values (block!, string!, ...)?

>> f: func [/local b s] [b: [] s: "" insert b 1 insert s 1]
>> f
== ""
>> f
== "1"
>> source f

f: make function! [[/local b s][b: [1 1] s: "11" insert b 1 insert 
s 1]]

>> g: closure [/local b s] [b: [] s: "" insert b 1 insert s 1]
>> g
== ""
>> g
== ""
>> source g

g: make closure! [[/local b s][b: [] s: "" insert b 1 insert s 1]]

Souldn't the functionality be the other way around?
Group: !Cheyenne ... Discussions about the Cheyenne Web Server [web-public]
Dockimbel:
9-Jun-2007
Cheyenne release v0.9.14 beta. Download at http://softinnov.org/tmp/cheyenne-r0914.zip

Changelog :

o response/forward improved : 

 - fully supports URLs as argument (can now forward to another virtual 
 host).
	- URL validity check (must have an explicit target).
	- protection against cycles.


o Command line option -p extended, now you can specify several listen 
ports separated
   by a comma (ex: -p 80,10443).


o New command line option -e : load and initialize Cheyenne without 
entering the

   event loop (needed for embedding Cheyenne in third party apps).


o Added a new experimental module: mod-embed. Purpose is to allow 
easy Cheyenne

   integration in third-party REBOL applications that require an embedded 
   web
   server. (Uncomment mod-embed in httpd.cfg file to activate it)


o Added %embed-demo.r file to show a sample of the mod-embed usage 
and API. 

o RSP: <% without %> eats all the memory. Fixed.

o URL-encoded request values were not parsed correctly. Fixed.


o RSP: fixed a typo in 'decode-params blocking the multipart data 
decoding and
   also a local word ('type) leaking in GC.

o UniServe's service startup refactored to be more flexible.



The new mod-embed is experimental. Please look at the %embed-demo.r 
file and send your feedbacks here.
Group: !REBOL3 Schemes ... Implementors guide [web-public]
Andreas:
9-Jan-2010
as a context. with the bind i can make those values appear as if 
they were local words, i.e. buffer refers to event/port/locals/buffer
Group: !REBOL3 ... [web-public]
Sunanda:
5-Feb-2010
That produces an error for higher values of 2.

This should produce an int when it can, and a dec when it can't, 
though only for positive values of 2:

   pow: func [a b /local res][attempt [res: to-integer res: power a 
   b] res]
BrianH:
4-Mar-2010
Andreas, I don't have a problem with that solution in principle. 
It's just that it wouldn't work, and wouldn't be task-safe. The handlers 
for those functions would be task-local, the code blocks not. Plus 
it would break code that uses code block references rather than nested 
blocks, code that uses those functions through function values, and 
any function with the [throw] attribute (which we will be getting 
back in R3 with different syntax), and all of those exist in R3 mezzanine 
code. Plus there's all the extra BIND/copy overhead added to every 
call to loop functions, startup code, etc., and don't think that 
you won't notice that because that can double the memory usage and 
executiion time, at least.


The solution I proposed in the ticket comments is to have DO, CATCH 
and the loops set a task-local flag in the interpreter state when 
the relevant functions become valid, and unset it when they become 
invalid, then have the functions check the flag at runtime before 
they do their work (which they could because they're all native). 
This would be task-safe, only add a byte of task-local memory overhead, 
plus the execution overhead of setting and getting bits in that byte 
in a task-local way. It's the execution overhead that we don't know 
about, whether it would be too much. It would certainly be less than 
your proposal though.
1 / 122[1] 2